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

Why does it say that variable part is a non-existing Nil value?

Asked by 6 years ago

Im using a localscript:

game.Players.PlayerAdded:connect(function(player) sound = Instance.new("Sound",player.PlayerGui)

sound.SoundId = "rbxassetid://12222005"

part = game.Workspace.Music canplay = true

function muziekspelen(otherpart)

local humanoid = otherpart.Parent.Humanoid

if humanoid and canplay then
    sound:Play()
    canplay = false
    sound.Looped = true
    print(otherpart)
end

end

canplay = true end) part.Touched:connect(muziekspelen)

0
It does exist in the workspace btw kattenverzorger 23 — 6y
0
can you put your code in a codeblock correctly? Le_Teapots 913 — 6y
0
please accept my answer if it helped you Le_Teapots 913 — 6y

2 answers

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

Your part.Touched event is not triggering because of how you positioned it inside your script. Don't have it inside your Players.PlayerAdded event.

EDIT:

Your sound was not being copied to the PlayerGui because the server cannot access it due to how FilteringEnabled works. You would have to use RemoteEvents or RemoteFunctions for that. Lucky for you there's no need for that in your case: just manually put your sound inside StarterGui in studio.

However you will still need to have a RemoteEvent call FireClient() when your part gets pressed, then have a LocalScript play your sound:

Inside a regular script:

local part = game.Workspace.Music
local RemoteEvent = Instance.new("RemoteEvent", part)
canplay = true 

local function muziekspelen(otherpart)
    print(otherpart)
    local humanoid = otherpart.Parent:FindFirstChild("Humanoid")

    if humanoid and canplay then
        local player = game.Players:GetPlayerFromCharacter(otherpart.Parent)
        canplay = false
        RemoteEvent:FireClient(player)
    end

    canplay = true
end

part.Touched:connect(muziekspelen)

Inside a local script inside StarterGui:

local RemoteEvent = game.Workspace:WaitForChild("Music"):WaitForChild("RemoteEvent")
local player = game.Players.LocalPlayer

RemoteEvent.OnClientEvent:Connect(function()
    local sound = player.PlayerGui:WaitForChild("sound")
    sound:Play()
    sound.Looped = true
    print(otherpart)
end)
0
that worked out thanks kattenverzorger 23 — 6y
0
you are welcome fam Le_Teapots 913 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
game.Players.PlayerAdded:connect(function(player)
    sound = Instance.new("Sound",player.PlayerGui)


sound.SoundId = "rbxassetid://12222005"


local part = game.Workspace.Music
canplay = true 

local function muziekspelen(otherpart)
    print(otherpart)
    local humanoid = otherpart.Parent.Humanoid

    if humanoid and canplay then
        sound:Play()
        canplay = false
        sound.Looped = true
        print(otherpart)
    end



end


canplay = true

part.Touched:connect(muziekspelen)

end)


It doesn't give any errors anymore, but it still doesn't play the sound

0
and it does not print the otherpart while touched kattenverzorger 23 — 6y
0
And there is no sound in the PlayerGui folder, where i wanted it to be kattenverzorger 23 — 6y

Answer this question