Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago

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.

0
On line 5 try doing: "local player = game:GetService("Players"):FindFirstChild(hit.Parent.Name)" MizeryGFX 15 — 6y
0
GetPlayerFromCharacter is recommended and more reliable that than method Mizery. What is script.Parent TheFirstPire? RockerCaleb1234 282 — 6y
0
The scripts parent is a Part. Also im still very new to scripting so what would I change to GetPlayerFromCharacter? User#18068 0 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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!

0
No, audio does not need to be in workspace. hiimgoodpack 2009 — 6y
0
This script didn't work in game (only in test) but It lead me to my answer so ill just accept it anyway. User#18068 0 — 6y
Ad

Answer this question