I have 2 problems with this script one in line 3 and the other in line 5. The error for line 3 is "workspace.Part.MusicScript1:3:attempt to index field parent (a nil value)" in line 5 it's "workspace.Part.MusicScript1:5:attempt to index field player (a nil value)"
What do these 2 errors mean and how do I fix them?
Here is the full script-
script.Parent.Touched:Connect(function(hit) wait(1) if hit.Parent:FindFirstChild('Humanoid') then local player = game:GetService('Players'):GetPlayerFromCharacter(hit.Parent) local playergui = player:WaitForChild('PlayerGui') local Song1 = playergui:WaitForChild('Song1') local Song2 = playergui:WaitForChild('Song2') Song1.Volume = 1 Song2:Stop() Song1:Play() end end)
Also the Audio the script is trying to play is in StarterGui and the script will work in test mode but not in game.
Ok, so using the exact same script inside a part in workspace, I did not get any errors. I think music needs to be in the workspace
in order to play (I don't have much experience with audio in roblox).
I put the songs into the replicated storage and created a chosensong that I put into workspace that I wanted to play.
I am not sure how you encountered your errors, but if they continue to appear please let me know.
local ReplicatedStorage = game:GetService("ReplicatedStorage") script.Parent.Touched:Connect(function(hit) wait(1) if hit.Parent:FindFirstChild('Humanoid') then local player = game:GetService('Players'):GetPlayerFromCharacter(hit.Parent) --Don't need the PlayerGui with this setup. local Song1 = ReplicatedStorage:WaitForChild('Song1') local Song2 = ReplicatedStorage:WaitForChild('Song2') local ChosenSong = Song1 ChosenSong.Parent = workspace ChosenSong.Volume = 1 Song2:Stop() ChosenSong:Play() end end)
If this fixed your problem please remember to accept my answer. If this does not solve your problem please comment on what I could have done better if you know my mistake, or just let me know via a comment or a message what the error is. Thanks!