I’ve made a script for my obby that changes the color of the checkpoint and emits some particles when touched, its a local script put inside StarterPlayerScripts but for some reason it changes colors for everyone
My problem is that the brick color change and particle emitters happen to all clients. I just want to it to happen to individual who touched the brick
When I tested the game, I noticed that it does in fact change the checkpoint to green on the client. and the server still shows the normal color. But every player’s checkpoint, even if they haven’t gotten to it, will change to green.
for i,checkpoint in pairs(game.Workspace.Checkpoints:GetChildren()) do
if checkpoint.Name ~= "1" then
-- particle emiters
local p1 = checkpoint.Arrow:WaitForChild("a")
local p2 = checkpoint.Arrow:WaitForChild("b")
local p3 = checkpoint.Arrow:WaitForChild("c")
local p4 = checkpoint.Arrow:WaitForChild("d")
local p5 = checkpoint.Arrow:WaitForChild("e")
local p6 = checkpoint.Arrow:WaitForChild("f")
-- Checkpoint Outlines
local o1 = checkpoint:WaitForChild("OutlineA")
local o2 = checkpoint:WaitForChild("OutlineB")
local o3= checkpoint:WaitForChild("OutlineC")
local o4 = checkpoint:WaitForChild("OutlineD")
local debounce = checkpoint:WaitForChild("Debounce")
function onTouched(hit)
if debounce.Value == true then
debounce.Value = false
p1:emit(50)
p2:emit(50)
p3:emit(50)
p4:emit(50)
p5:emit(50)
script.Sound:Play()
o1.BrickColor = BrickColor.new("Lime green")
o2.BrickColor = BrickColor.new("Lime green")
o3.BrickColor = BrickColor.new("Lime green")
o4.BrickColor = BrickColor.new("Lime green")
checkpoint.Arrow.StageArrow.Texture = "http://www.roblox.com/asset/?id=14157770728"
end
end
checkpoint.Touched:connect(onTouched)
end
end
This is because the touched event will trigger for any player (or object) that touches the checkpoint. You need to check if the “hit” variable is a player and if so: check if it is the client’s player.
You can use the Players service. An example to get the LocalPlayer (client’s player) can be seen below:
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
From there, you would just compare if the player that is hit is the same as the “Player” variable. @TilenT1 gave a great example to verify that the checkpoint was touched by an object with a humanoid (usually players)! All you would have to do is get the player object from the character using GetPlayerFromCharacter() and compare the two variables
It doesn’t seem to be working, now it doesn’t even change the color?
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
for i,checkpoint in pairs(game.Workspace.Checkpoints:GetChildren()) do
if checkpoint.Name ~= "1" then
-- particle emiters
local p1 = checkpoint.Arrow:WaitForChild("a")
local p2 = checkpoint.Arrow:WaitForChild("b")
local p3 = checkpoint.Arrow:WaitForChild("c")
local p4 = checkpoint.Arrow:WaitForChild("d")
local p5 = checkpoint.Arrow:WaitForChild("e")
local p6 = checkpoint.Arrow:WaitForChild("f")
-- Checkpoint Outlines
local o1 = checkpoint:WaitForChild("OutlineA")
local o2 = checkpoint:WaitForChild("OutlineB")
local o3= checkpoint:WaitForChild("OutlineC")
local o4 = checkpoint:WaitForChild("OutlineD")
local debounce = checkpoint:WaitForChild("Debounce")
function onTouched(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
if Player == hit then
if debounce.Value == true then
debounce.Value = false
p1:emit(50)
p2:emit(50)
p3:emit(50)
p4:emit(50)
p5:emit(50)
script.Sound:Play()
o1.BrickColor = BrickColor.new("Lime green")
o2.BrickColor = BrickColor.new("Lime green")
o3.BrickColor = BrickColor.new("Lime green")
o4.BrickColor = BrickColor.new("Lime green")
checkpoint.Arrow.StageArrow.Texture = "http://www.roblox.com/asset/?id=14157770728"
end
end
end
end
checkpoint.Touched:connect(onTouched)
end
end
The hit variable is the part that touched the checkpoint. In your case, it is probably the left or right leg. The parent of the hit variable should be the character of the player (Not always the case if the hit variable is something like an accessory but it shouldn’t matter much here). In order to get the player you need to use the GetPlayerFromCharacter() function. An example can be seen below:
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Plr = Players:GetPlayerFromCharacter(hit.Parent)
if Player == Plr then
-- Change color
end
Ahh I gachu, I have a very slow PC so before I test it does this look right?:
for i,checkpoint in pairs(game.Workspace.Checkpoints:GetChildren()) do
if checkpoint.Name ~= "1" then
-- particle emiters
local p1 = checkpoint.Arrow:WaitForChild("a")
local p2 = checkpoint.Arrow:WaitForChild("b")
local p3 = checkpoint.Arrow:WaitForChild("c")
local p4 = checkpoint.Arrow:WaitForChild("d")
local p5 = checkpoint.Arrow:WaitForChild("e")
local p6 = checkpoint.Arrow:WaitForChild("f")
-- Checkpoint Outlines
local o1 = checkpoint:WaitForChild("OutlineA")
local o2 = checkpoint:WaitForChild("OutlineB")
local o3= checkpoint:WaitForChild("OutlineC")
local o4 = checkpoint:WaitForChild("OutlineD")
local debounce = checkpoint:WaitForChild("Debounce")
function onTouched(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Plr = Players:GetPlayerFromCharacter(hit.Parent)
if Player == Plr then
if debounce.Value == true then
debounce.Value = false
p1:emit(50)
p2:emit(50)
p3:emit(50)
p4:emit(50)
p5:emit(50)
script.Sound:Play()
o1.BrickColor = BrickColor.new("Lime green")
o2.BrickColor = BrickColor.new("Lime green")
o3.BrickColor = BrickColor.new("Lime green")
o4.BrickColor = BrickColor.new("Lime green")
checkpoint.Arrow.StageArrow.Texture = "http://www.roblox.com/asset/?id=14157770728"
end
end
end
end
checkpoint.Touched:connect(onTouched)
end
end
Of course! The hierarchy of an accessory looks like this:
So, an easy solution would just be to check if the parent of the parent of the hit variable has a humanoid as well (hope that didn’t sound too confusing). Here’s an example:
local humanoid = hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChild("Humanoid")
if humanoid then
-- Proceed
end