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

why isn't my script executing? it doesn't even give me any errors in the output.

this script is supposed to make it so that, when you step on a brick, music plays for the player who stepped on it.

game.Players.PlayerAdded:Connect(function(plr)
	local function onTouched(hit)
		local sound = Instance.new("Sound", plr.PlayerGui)
		sound.SoundId = "rbxassetid://1381855428"
	end
	script.Parent.Touched:Connect(onTouched)
	end)

3 comments

0

Is this a local script?

0

Why would it be? It's a server script.

0

check again if the soundId matches the link

1 answer

✓ Accepted answer

The PlayerAdded function is totally unnecessary. If you want a sound to play on the client, then you should use RemoteEvents and FireClient() Also, if you're gonna use Instance.new() every single time, it will take resources and eventually cause a memory leak (Which is gonna break your game into nothing but spongecake.) Anyways, put a remote event in ReplicatedStorage and put a script under the part. As for the local script, you can put it anyways, but I suggest StarterPlayerScripts.

Server Script

local part = script.Parent
local remoteEvent = game.ReplicatedStorage.RemoteEvent

part.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent) --This gets the player, and only the player.
	if plr then
		remoteEvent:FireClient(plr) --Your first parameter is always the specific player.
	end
end)

Local Script

local remoteEvent = game.ReplicatedStorage.RemoteEvent
local sound = game.ReplicatedStorage.Sound

remoteEvent.OnClientEvent:Connect(function() --You do not need the first parameter mentioned before.
	sound:Play()
end)

1 comment

0

thank you so much :D

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