I had this idea for the sound to change when a player is in a certain area, which is changed when a player touches a part. For some reason it's not working.
error: Workspace.Area:12: attempt to call global 'onPlayerRespawned' (a nil value)
local function onPlayerEntered(newPlayer) local stats = Instance.new("StringValue") stats.Name = "Area" stats.Value = "Spawn" stats.Parent = newPlayer end function onPlayerEntered(newPlayer) newPlayer.Changed:connect(function (property) if (property == "Character") then onPlayerRespawned(newPlayer) local music = Instance.new("Sound",newPlayer.PlayerGui) music.Looped = true local Id = "rbxassetid://" music.SoundId = Id.."319731744" music.Volume = .5 music:Play() local stat = newPlayer:FindFirstChild("Area") if stat.Value == "Lobby" then music.SoundId = Id.."326012211" elseif stat.Value == "Hi" then music.SoundId = Id.."314069452" end end end) end game.Players.ChildAdded:connect(onPlayerEntered)
Please help with explainations?
There are a few small errors in your script. The main thing is that in the line you were trying to call the function, there is no function called: onPlayerRespawned(newPlayer). You can either cut it out, or create a new function within or outside of the parent function. Also, if you want the sound to play correctly, you'd have to control how many sound files are being created. You'd either create a new one every time, then delete the existing one, or create multiple sounds and make them active or deactivate at certain times. Here's what all of this should look like:
local function onPlayerEntered(newPlayer) local stats = Instance.new("StringValue") stats.Name = "Area" stats.Value = "Spawn" stats.Parent = newPlayer end function onPlayerRespawned(newPlayer) local music = newPlayer:FindFirstChild("Sound") if music == nil then local music = Instance.new("Sound", newPlayer.PlayerGui) music.Looped = true local Id = "rbxassetid://" music.SoundId = Id.."319731744" music.Volume = .5 music:Play() elseif music ~= nil then music.Looped = true local Id = "rbxassetid://" music.SoundId = Id.."319731744" music.Volume = .5 music:Play() end local stat = newPlayer:FindFirstChild("Area") if stat.Value == "Lobby" then music.SoundId = Id.."326012211" elseif stat.Value == "Hi" then music.SoundId = Id.."314069452" end end function onPlayerEntered(newPlayer) newPlayer.Changed:connect(function (property) if (property == "Character") then onPlayerRespawned(newPlayer) end end) end game.Players.ChildAdded:connect(onPlayerEntered)
Hopefully this gave you a start to your script. I've only edited the parts that need working on, so not everything everything is garenteed to work. If have any questions, feel free to put a comment below. Otherwise, I have some links that'll hopefully better explain to you the kind of concepts I'm trying to cover. YOUR WELCOME!!
LINKS:
Roblox Wiki: http://wiki.roblox.com/index.php/Scripting
Sound: http://wiki.roblox.com/index.php?title=API:Class/Sound
functions: http://wiki.roblox.com/index.php?title=Function
The Player
: http://wiki.roblox.com/index.php?title=API:Class/Player