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
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
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
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.
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)
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
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