Hello so today i just learn roblox studio to create a game. Im making a script where certain music will play if the player hit / enter the area and if the player leave the area music will also change. I tried to create the script but i got an error, the error is the background music does not stop and the elevator music does not play when player leave the part / area. Is there something wrong the code ?
local SoundService = game:GetService("SoundService")
local backgroundMusic = SoundService.BackgroundMusic
local elevatorMusic = SoundService.Elevator
local part = workspace.InsideBuilding
local musicPlayed = false
local CurrentArea = nil
elevatorMusic:Play()
part.Touched:Connect(function(hit)
local character = game.Players:playerFromCharacter(hit.Parent)
if character:IsA("Player") and character then
if part ~= CurrentArea then
CurrentArea = part
elevatorMusic:Stop()
if not backgroundMusic.IsPlaying then
backgroundMusic:Play()
end
elseif CurrentArea == nil then
backgroundMusic:Stop()
elevatorMusic:Play()
end
end
end)
The local part is the floor of the building.
You should Run the part when the music plays when the player touches the part:
also you did the touching mechanism wrong
local SoundService = game:GetService("SoundService")
local backgroundMusic = SoundService.BackgroundMusic
local elevatorMusic = SoundService.Elevator
local part = workspace.InsideBuilding
local musicPlayed = false
local CurrentArea = nil
part.Touched:Connect(function(hit)
if hit.parent:findfirstchild("Humaniod")
elevatorMusic:Play()
if part ~= CurrentArea then
CurrentArea = part
elevatorMusic:Stop()
if not backgroundMusic.IsPlaying then
backgroundMusic:Play()
end
elseif CurrentArea == nil then
backgroundMusic:Stop()
elevatorMusic:Play()
end
end
end)
hope this helped you
Related
I have a problem with my Roblox combat system. The problem is that my Animations don't play when I play them. Here are the scripts:
Client Script
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local UserInputService = game:GetService("UserInputService")
local Debounce = 0.5
local Keybind = Enum.KeyCode.F
local CanPunch = true
local count = 1
local Animations =
{
script:WaitForChild("PunchAnim"),
script:WaitForChild("PunchAnim2")
}
UserInputService.InputBegan:Connect(function(Input, busy)
if Input.KeyCode == Keybind and not busy then
print("Keybind Check")
if CanPunch == true then
print("CanPunch Check")
CanPunch = false
local Anim = char.Humanoid.Animator:LoadAnimation(Animations[count])
Anim:Play()
Anim.Looped = false
count = (count%#Animations) + 1
print("Anim Played")
game.ReplicatedStorage.remotes.Punch:FireServer(player, char)
print("Fired Event")
wait(Debounce)
CanPunch = true
end
end
end)
Server Script
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
hitbox = Instance.new("Part", workspace)
hitbox.Size = Vector3.new(4,4,4)
hitbox.CanCollide = false
hitbox.Transparency = 1
local weld = Instance.new("Weld", hitbox)
weld.Part0 = char.HumanoidRootPart
weld.Part1 = hitbox
weld.C1 = CFrame.new(0,0,4)
end)
end)
game.ReplicatedStorage.remotes.Punch.OnServerEvent:Connect(function(player, char)
for i, v in pairs(workspace:GetPartsInPart(hitbox)) do
if v.Parent:FindFirstChild("Humanoid") and v.Parent ~= char and v.Parent:FindFirstChild("Hit"..player.Name) == nil then
local Debounce = Instance.new("IntValue", v.Parent)
Debounce.Name = "Hit"..player.Name
game.Debris:AddItem(Debounce, 0.25)
v.Parent:FindFirstChild("Humanoid"):TakeDamage(7.5)
end
end
end)
In the script, I'm telling the Animations to play after F is pressed but when I press it in the game, it doesn't play the animation.
I made this script by watching various tutorials to make my own combat system. I've tried fixing the code by re-watching the tutorials as well as reading the roblox documentation to find a solution but I couldn't find one.
I'm new to Lua and I apologize in advance if this is a very easy and basic question, but all help is appreciated. Also, please point out any other mistake in my script. Thanks.
If you see this question, please answer it if you can.
The answer of my question was, that the animations were R6 but my character was R15. I went and changed the game settings and made it so the game only supported R6.
It keeps returning nil for player and saying that im trying to index a nil with 'WaitForChild' even though I have tried adding a wait command and changing player to 'game.Players.LocalPlayer' I'm new to scripting and I don't know what else to do.
local buyButton = script.Parent
local player = game:GetService("Players").LocalPlayer
local multiplier = player:WaitForChild("Multiplier")
buyButton.MouseButton1Up:Connect(function()
if multiplier.Value == 0 then
multiplier.Value = 1
end
end)
Instead of local player = game:GetService("Players").LocalPlayer you want to do:
local players = game:GetService("Players")
local player = players.localplayer
So, Im tring to make a Mind Control script inside of Roblox. Im using the local script so i can use the UserInputServer Module. But When I Run It(Its inside of StartGui), I doesn't give any output. Here is the source:
--Made By BioShot!--
local UIS = game:GetService("UserInputService")
local Enabled = false
local Active = false
game.Players.PlayerAdded:Connect(function(plr)
print("Player Added!")
UIS.InputBegan:Connect(function(input)
if(input['KeyCode'] == Enum['KeyCode']["E"]) then
if(Enabled == true) then
--Rase Player/Dummy/NPC
local Mouse = plr:GetMouse()
Active = true
while(Active == true) do
local Enemy = Mouse["Target"]
print(Enemy)
Enemy.CFrame = Enemy.CFrame + CFrame.new(0,1,0)
wait(0.5)
end
else
--Drop Player/Dummy/NPC
end
end
end)
UIS.InputEnded:Connect(function(input)
if(input['KeyCode'] == Enum["KeyCode"]["F"]) then
Enabled = true
print("Enabled.")
end
if(input['KeyCode'] == Enum["KeyCode"]["E"]) then
Active = false
end
end)
end)
The statements are only printed after game.Players.PlayerAdded:Connect(function(plr) detects a player being added. Due to the code running in a LocalScript, the player will have already been added by the time the script starts waiting for players. If a second player joins the game, "Player Added!" will indeed be printed in the first player's console.
The player can instead be indexed by using local plr = game:GetService("Players").LocalPlayer.
The script still errors when the player's mouse is pointing to nothing and when trying to change the Enemy's position, but these can be easily fixed. The script then lifts objects upwards by hovering over them and pressing E.
Final code:
--Made By BioShot!--
local UIS = game:GetService("UserInputService")
local Enabled = false
local Active = false
local plr = game:GetService("Players").LocalPlayer
print("Player Added!")
UIS.InputBegan:Connect(function(input)
if(input['KeyCode'] == Enum['KeyCode']["E"]) then
if(Enabled == true) then
--Rase Player/Dummy/NPC
local Mouse = plr:GetMouse()
Active = true
while(Active == true) do
local Enemy = Mouse["Target"]
if Enemy then --Make sure the player's mouse isn't pointing to nothing
print(Enemy)
Enemy.CFrame += Vector3.new(0,1,0)
end
task.wait(0.5) --More accurate than wait(0.5)
end
else
--Drop Player/Dummy/NPC
end
end
end)
UIS.InputEnded:Connect(function(input)
if(input['KeyCode'] == Enum["KeyCode"]["F"]) then
Enabled = true
print("Enabled.")
end
if(input['KeyCode'] == Enum["KeyCode"]["E"]) then
Active = false
end
end)
I've been learning how to change the player's chat color and tags, the only thing I want to achieve now is to change it back to the default
Here's the code (I'm only gonna include the important ones)
ceValue.Changed:Connect(function()
local player = game.Players:WaitForChild(ceValue.Value)
local character = player.Character
while character do
if chatService ~= nil then
local speaker = chatService:GetSpeaker(player.Name)
repeat wait(1)
speaker = chatService:GetSpeaker(player.Name)
until speaker ~= nil
speaker:SetExtraData("NameColor", ceColor)
speaker:SetExtraData("ChatColor", ceColor)
if ceValue.Value == "" then
for i, v in pairs(game.Players:GetChildren()) do
speaker = chatService:GetSpeaker(v.Name)
--///HERE'S WHERE I WANT TO CHANGE THE PLAYER'S CHAT COLOR///---
end
break
end
wait(0.5)
end
end
end)
Each Chat Speaker is assigned a color on its creation based off its username. This way, each Speaker won't get a different color if they leave and join another game.
There are a couple of different methods where you can retrieve a default color of a speaker.
Getting the Default Color
Save It
Once the player joins, you can cache their color and then allow them to modify it. For this method, you have to make sure that you don't allow the player to modify their color beforehand - otherwise, you'll end up saving the wrong color.
This code snippet would grab what would be their default color and store that instead using ComputeNameColor(player.Name) from the chatService.
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
local color = ComputeNameColor(player.Name)
local colorObj = Instance.new("Color3Value")
colorObj.Name = "DefaultChatColor"
colorObj.Value = color
colorObj.Parent = player
end)
Chat colors do consist of BrickColor and Color3 values, but the BrickColor.new("x").Color property used in Roblox's color dictionary to reference these BrickColor items will convert them into a Color3 anyway.
Later on, you can retrieve the chat color again by just calling for the player.DefaultChatColor.Value.
Regenerate It
If you can't retrieve the color from the Color3Value, you can always generate the default color of a speaker the same way Roblox does - performing math on the username! You'll want to call GetNameColor(Speaker) to get a speaker's team/default color, or ComputeNameColor(player.Name) if you want to ignore teams altogether.
The code that generates the default colors is present within game.Chat.DefaultChatModules.ExtraDataInitializer on lines 86-127 (where it looks like this code is running inside due to the variable names), but you can clone it if you're working inside a GUI or somewhere else:
local NAME_COLORS =
{
Color3.new(253/255, 41/255, 67/255), -- BrickColor.new("Bright red").Color,
Color3.new(1/255, 162/255, 255/255), -- BrickColor.new("Bright blue").Color,
Color3.new(2/255, 184/255, 87/255), -- BrickColor.new("Earth green").Color,
BrickColor.new("Bright violet").Color,
BrickColor.new("Bright orange").Color,
BrickColor.new("Bright yellow").Color,
BrickColor.new("Light reddish violet").Color,
BrickColor.new("Brick yellow").Color,
}
local function GetNameValue(pName)
local value = 0
for index = 1, #pName do
local cValue = string.byte(string.sub(pName, index, index))
local reverseIndex = #pName - index + 1
if #pName%2 == 1 then
reverseIndex = reverseIndex - 1
end
if reverseIndex%4 >= 2 then
cValue = -cValue
end
value = value + cValue
end
return value
end
local color_offset = 0
local function ComputeNameColor(pName)
return NAME_COLORS[((GetNameValue(pName) + color_offset) % #NAME_COLORS) + 1]
end
local function GetNameColor(speaker)
local player = speaker:GetPlayer()
if player then
if player.Team ~= nil then
return player.TeamColor.Color
end
end
return ComputeNameColor(speaker.Name)
end
Applying
Applying the new, or old in this case, chat color should be identical to what you've done in the code already: using speaker:setExtraData("(x)Color", oldColor).
"Regenerate It"
ceValue.Changed:Connect(function()
local player = game.Players:WaitForChild(ceValue.Value)
local character = player.Character
while character do
if chatService ~= nil then
local speaker = chatService:GetSpeaker(player.Name)
repeat wait(1)
speaker = chatService:GetSpeaker(player.Name)
until speaker ~= nil
speaker:SetExtraData("NameColor", ceColor)
speaker:SetExtraData("ChatColor", ceColor)
if ceValue.Value == "" then
for i, v in pairs(game.Players:GetChildren()) do
speaker = chatService:GetSpeaker(v.Name)
defColor = GetNameColor(Speaker)
speaker:SetExtraData("NameColor", defColor)
speaker:SetExtraData("ChatColor", defColor)
end
break
end
wait(0.5)
end
end
end)
"Store it!"
ceValue.Changed:Connect(function()
local player = game.Players:WaitForChild(ceValue.Value)
local character = player.Character
while character do
if chatService ~= nil then
local speaker = chatService:GetSpeaker(player.Name)
repeat wait(1)
speaker = chatService:GetSpeaker(player.Name)
until speaker ~= nil
speaker:SetExtraData("NameColor", ceColor)
speaker:SetExtraData("ChatColor", ceColor)
if ceValue.Value == "" then
for i, v in pairs(game.Players:GetChildren()) do
speaker = chatService:GetSpeaker(v.Name)
local cachedCol = v:FindFirstChild("DefaultChatColor")
if cachedCol ~= nil then
--There's a cache found.
speaker:SetExtraData("NameColor",cachedCol.Value)
speaker:SetExtraData("ChatColor",cachedCol.Value)
end
end
break
end
wait(0.5)
end
end
end)
Hope this helps!
I am using WorldToScreenPoint and GetPartsObscuringTarget. What I am trying to fix is that a player must detect parts of player on a different team. For example, there is a player on a team alone trying to find other players by detecting one of their humanoid parts. The script only detect parts inside the Workspace.
I've tried using Player.CharacterAdded:Wait() and a nested loop to go through players if they are on the same team or not. This didn't worked. I've been trying to fix this for the past 2 weeks.
local Player = game.Players.LocalPlayer
local Character = workspace:FindFirstChild(Player.Name)
game:GetService("RunService").Heartbeat:Connect(function()
for _, object in pairs(Character:GetDescendants()) do
if object:IsA("Part") or object:IsA("MeshPart") then
if not object:IsDescendantOf(game.Players.LocalPlayer.Character) then
local object2,visible = workspace.CurrentCamera:WorldToScreenPoint(object.Position)
if visible == true then
local pos1 = workspace.CurrentCamera.CFrame.Position
local pos2 = object.Position
local castPoints = {pos1,pos2}
local ignoreList = {game.Players.LocalPlayer.Character, object}
local obstructs = workspace.CurrentCamera:GetPartsObscuringTarget(castPoints,ignoreList)
if obstructs[1] then
return
elseif not obstructs[1] then
print(object.Name.." has been detected!")
end
elseif visible ~= true then
print("You are now incognito.")
return
end
end
end
end
end)