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!
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:
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?
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.”
-- 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)