Brick Color Change Client Side?

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.

Cleints:

Server:
image

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
9 Likes

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.

6 Likes

How would I check if it’s the client’s player?

5 Likes

Example of a function:

local function onTouched(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
		-- Enter what happens here
	end
end)
6 Likes

Would I also have to check if it’s the client’s player as well or just any humanoid?

5 Likes

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 :slightly_smiling_face:

5 Likes

Alright I’ll try that right now! :slight_smile:

3 Likes

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

4 Likes

Here is the issue:

if Player == hit then

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
4 Likes

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
4 Likes

Yep! Looks right! If you have any issues let me know :slightly_smiling_face:

2 Likes

Alrighty I’ll test it, and let you know! :slight_smile:

2 Likes

100% works!! Thanks so much for the help. :grinning:

You were saying before that if it hits an accessory, it wouldn’t work. Is there an easy way to fix this? (Because new layered clothing, etc)

3 Likes

Of course! The hierarchy of an accessory looks like this:
image
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
3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.