How to make local color change when player touches part?

Im trying to make it so whenever a player touches a checkpoint, the color changes, but the color only changes for that player. I have a localscript which is in a folder in StarterPlayerScripts which runs the color change which does not work for some reason. Here is the localscript:
local player = game.Players.LocalPlayer
player:WaitForChild(“leaderstats”)

local CheckpointsFolder = workspace:WaitForChild(“Checkpoints”)

local function updateCheckpointColors()
for _, part in ipairs(CheckpointsFolder:GetDescendants()) do
if part:IsA(“BasePart”) and part.Parent == CheckpointsFolder then
local stageNumber = tonumber(part.Name)
if stageNumber and game.Players.LocalPlayer.leaderstats.Stage.Value >= stageNumber then
part.BrickColor = BrickColor.new(“Bright green”)
end
end
end
end

updateCheckpointColors()

player.leaderstats.Stage.Changed:Connect(updateCheckpointColors)

Also I have a CheckPointScript in ServerScriptService if that matters:
local Rs = game.ReplicatedStorage
local Event = Rs:WaitForChild(“Events”)

game.Players.PlayerAdded:Connect(function(Player)
local leaderstats = Instance.new(“Folder”)
leaderstats.Parent = Player
leaderstats.Name = “leaderstats”

local Stage = Instance.new("NumberValue")
Stage.Parent = leaderstats
Stage.Name = "Stage"
Stage.Value = 0

local CheckpointsFolder = game.Workspace:FindFirstChild("Checkpoints")

for i, Checkpoint in pairs(CheckpointsFolder:GetChildren()) do
	Checkpoint.Touched:Connect(function(Hit)
		if Hit.Parent:FindFirstChild("Humanoid") then
			local PlayerHit = game.Players:GetPlayerFromCharacter(Hit.Parent)

			Checkpoint.Color = Color3.fromRGB(75, 151, 75)
			Checkpoint.Material = Enum.Material.Neon

			if PlayerHit.leaderstats:FindFirstChild("Stage").Value == Checkpoint.Name - 1 then
				PlayerHit.leaderstats:FindFirstChild("Stage").Value = Checkpoint.Name
				workspace.SoundFx.Success:Play()
				Event.CheckPointEvent:FireClient(Player)
			end
		end
	end)

	Player.CharacterAdded:Connect(function(Character)
		repeat
			wait()
		until Character:FindFirstChild("HumanoidRootPart") ~= nil

		local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
		HumanoidRootPart.CFrame = CheckpointsFolder:FindFirstChild(Stage.Value).CFrame + Vector3.new(0, 2, 0)
	end)
end

end)

2 Likes

Why don’t you just put a local script in the part and do

Script.Parent.Touched:Connect(Function(Hit)
     Script.Parent.BrickColor = rgb(--Insert rgb value)
end)
2 Likes

I have multiple checkpoints and that process will take a long time.
Is there a more efficient way I can do that?

You might be able to use a remote event when a player touches a checkpoint and have a similar script run.

1 Like

I inserted the script you sent me into each part, it didnt work. Another player can see the color change

I don’t know why, I don’t think I can help anymore. Sorry.

Alright, thank you for trying to help

i would put the sound in StarterGui so that the sound plays locally instead of globally

also the best way to achieve what you’re doing is to use a RemoteEvent to change the color of the part when the player touches it

1 Like

do you know how I would modify the script for using a RemoteEvent?

Try using collection service. Create a tag, and assign each checkpoint to that tag. Then, check if they have been touched through a script.

For example:

local c_service = game:GetService("CollectionService")

for _, part in c_service:GetTagged(checkpoint_tag_goes_here) do
 part.Touched:Connect(function(hit)
  if hit.Parent:FindFirstChild("Humanoid") then
   --remote event fire
  end
 end
end

Note: You don’t have to use remote events, as you could legit just simply make this in a different script. You could just access the player through the hit.Parent/Character

Hope this helps :cowboy_hat_face:

1 Like

Can you explain this to me step by step? Im kind of confused

You could just have 1 local script saying:


local function Detect()
for i, v in pairs(CheckpointsFolder:GetChildren()) do
v.Touched:Connect(function(hit)
if game.Players:GetPlayerFromCharacter(hit.Parent) == game.Players.LocalPlayer then
v.Color = -- Set colour
end
end)
end
end

while wait(0.05) do
Detect()
end

Sorry If there are any errors as I wrote this in a rush.

1 Like

Actually I think @SuperProgrammingBro has the better idea

1 Like

im just going to make a simple script for you since i think you are fairly new at scripting. put this code in a LocalScript and move it to StarterGui

local CheckpointsFolder = game.Workspace:FindFirstChild("Checkpoints")

for i, Checkpoint in pairs(CheckpointsFolder:GetChildren()) do
	Checkpoint.Touched:Connect(function(Hit)
		if Hit.Parent:FindFirstChild("Humanoid") then

Checkpoint.Color = Color3.fromRGB(75, 151, 75)
Checkpoint.Material = Enum.Material.Neon
 
end
end)
2 Likes

I certainly can!

Step 1, creating the tag: Go into your toolbox and click on plugins. In the search bar, search up “Tag Editor”. Click on the plugin by Sweetheartichoke. If it isn’t already opened, go to your plugins tab and search for a plugin named “Instance Tagging”. Click on it, and you will see two things dropdown, “Tag Window”, and “World View”. Click on the Tag Window one. Once you open it, click on the “Add a new tag” thing, and type in your tag name.

Step 2, assigning the tags: Open up the folder that stores all your checkpoints. Select every checkpoint. Still selecting all the checkpoints, look at your tag window. You will see a check next to the tag you created. Click the check. Now, all your checkpoints have been assigned a tag!

Step 3, scripting: Create a local script either inside StarterCharacterScripts or StarterGUI, and rename it to “Tag Handler”. Inside the script, put something similar to what I have here:

local c_service = game:GetService("CollectionService") --collection service is a service that handles all tags

for _, part in c_service:GetTagged(checkpoint_tag_goes_here) do --for every part that you assigned to the tag, the below will run
 part.Touched:Connect(function(hit) --If its a model, try creating a hitbox that surrounds the model and put `part.HitBox.Touched.`
  if hit.Parent:FindFirstChild("Humanoid") then
    part.Color = Color3.fromRGB(--your color goes here--) --If its a model, do a pairs() loop for this.
    part.Material = "Neon" --Same thing here.
   end
 end

Kinda long, but its aight. :cowboy_hat_face:

I used that local script and moved it to startergui but the color is still visible for everyone

1 Like

Just to make sure, is this what it’s supposed to look like at the end?

2 Likes

Also this is a local script which is in a folder in StarterPlayerScripts
I dont know if its going to affect anything

local player = game.Players.LocalPlayer
player:WaitForChild(“leaderstats”)

local CheckpointsFolder = workspace:WaitForChild(“Checkpoints”)

local function updateCheckpointColors()
for _, part in ipairs(CheckpointsFolder:GetDescendants()) do
if part:IsA(“BasePart”) and part.Parent == CheckpointsFolder then
local stageNumber = tonumber(part.Name)
if stageNumber and game.Players.LocalPlayer.leaderstats.Stage.Value >= stageNumber then
part.BrickColor = BrickColor.new(“Bright green”)
end
end
end
end

updateCheckpointColors()

player.leaderstats.Stage.Changed:Connect(updateCheckpointColors)

for the :GetTagged thing, make sure you put the tag’s name in quotation marks.

Does that do the same thing as what you’re looking for? Like where it changes color locally?