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

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

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)

3 comments

0

It does exist in the workspace btw

0

can you put your code in a codeblock correctly?

0

please accept my answer if it helped you

2 answers

✓ Accepted answer

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)

2 comments

0

that worked out thanks

0

you are welcome fam

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

2 comments

0

and it does not print the otherpart while touched

0

And there is no sound in the PlayerGui folder, where i wanted it to be

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