Need help with making local sounds

I am making an obby, and right now I’m trying to make it so when a player touches a checkpoint, it plays a sound. The issue I’m having is that it plays for everyone, when I just want it to play for that person. This is the code I have so far:

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid")then
		if game.Players:FindFirstChild(hit.Parent.Name)then
			local Player = game.Players:FindFirstChild(hit.Parent.Name)
			local Level = Player:WaitForChild("leaderstats"):WaitForChild("Level")
			if Level.Value == script.Parent.Name -1 then
				Level.Value = script.Parent.Name
				game.Workspace.CheckpointSound:Play()
				script.Parent.Color = Color3.new(1, 0.294118, 0.305882) --Changes the checkpoint color
				wait(1)
				script.Parent.Color = Color3.new(0, 0.615686, 0)
			end
		end
	end
end)

I have the sound in workspace, and the script under the checkpoint the player touches. Any help is appreciated, thanks!

https://developer.roblox.com/en-us/api-reference/function/SoundService/PlayLocalSound

1 Like

I took a look through the sample code, and it seemed like what I was looking for. Just to be sure, is the code a normal/local script, and where would I want to put it? Thanks.

Thanks so much for the reply! This seems to almost work. When touched, the checkpoint no longer lights up, and I’ve tested it with printing things, so I think that there’s something wrong with this line in the serverscript:

game.ReplicatedStorage:WaitForChild(“RemoteEvent”):Fire(hit.Parent)

if you could give me any advice on where to go from here, that would be great. Thanks!

will each checkpoint have the ability to have a unique sound? or are they all the same
also, are you storing sound instances somewhere or just using the sound id number?

They will all have the same sound, and I have the sound as an instance.

game.ReplicatedStorage:WaitForChild(“RemoteEvent”):FireClient(hit.Parent)

Sorry, i forgott to add this, replace Fire with FireClient

Thanks, but it still doesn’t work. After more testing, it’s still the same line-
If I leave it like that, (game.ReplicatedStorage:WaitForChild(“RemoteEvent”):FireClient(hit.Parent)), then it says “player argument must be a Player object.” If I change it to FireClient(hit.Parent.Name), then it says “Unable to cast value to Object.”

Thanks for your help, it’s almost working :slight_smile:

I will test it in Studio and send you the script

Here the Script with Explanation:

-- Script:

script.Parent.Touched:Connect(function(hit) -- Fire when hit
	if hit.Parent:FindFirstChild("Humanoid")then -- If found Humanoid
	-- if game.Players:FindFirstChild(hit.Parent)then -- You dont need this, u already using `FindFirstChild("Humanoid")`
		local Player = game.Players:GetPlayerFromCharacter(hit.Parent) -- Get Player from Character
		local Level = Player:WaitForChild("leaderstats"):WaitForChild("Level")
		if Level.Value == script.Parent.Name -1 then
			Level.Value = script.Parent.Name
			game.ReplicatedStorage:WaitForChild("RemoteEvent"):FireClient(Player, script.Parent) -- Fire RemoteEvent to Client to the Player and with the part
		end
		end
end)
-- Client: 

game.ReplicatedStorage:WaitForChild("RemoteEvent").OnClientEvent:Connect(function(part) -- Fires Function when RemoteEvent was fired.

	script.Parent:Play() -- Play Music
	
-- Changing the Color, i take this script out from ServerScript and put it in Localscript so just you can see the changing Color and the other Players not. If it would be in ServerScript all Player would see the changing Color:
	part.Color = Color3.new(1, 0.294118, 0.305882)
	wait(1)
	part.Color = Color3.new(0, 0.615686, 0)
	
end)

Thank you so much for your help!

1 Like

No problem