Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What does attempt to index field *blank* (a nil value) mean?

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.

3 comments

0

On line 5 try doing: "local player = game:GetService("Players"):FindFirstChild(hit.Parent.Name)"

0

GetPlayerFromCharacter is recommended and more reliable that than method Mizery. What is script.Parent TheFirstPire?

0

The scripts parent is a Part. Also im still very new to scripting so what would I change to GetPlayerFromCharacter?

1 answer

✓ Accepted answer

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!

2 comments

0

No, audio does not need to be in workspace.

0

This script didn't work in game (only in test) but It lead me to my answer so ill just accept it anyway.

This discussion is preserved as it was. New answers, comments, and votes are closed.