I’m making a checkpoint that whenever you touch it, it changes the color to green then back to black. I want it to do this for every new checkpoint block the player touches.
I put a Server Script inside of a part in the workspace property that has a touched connect function, and it acts as a checkpoint for the player. The only problem is that the checkpoint part color changes the first time you touch a checkpoint, but when you touch a new checkpoint it doesn’t work.
Here’s the code:
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local checkpoint = script.Parent
function onTouched(hit)
if hit and hit.Parent and hit.Parent:FindFirstChildOfClass("Humanoid") then
local player = Players:GetPlayerFromCharacter(hit.Parent)
local checkpointData = ServerStorage:FindFirstChild("CheckpointData")
if not checkpointData then
checkpointData = Instance.new("Folder")
checkpointData.Name = "CheckpointData"
checkpointData.Parent = ServerStorage
end
local userIdString = tostring(player.UserId)
local checkpointValue = checkpointData:FindFirstChild(userIdString)
if not checkpointValue then
checkpointValue = Instance.new("ObjectValue")
checkpointValue.Name = userIdString
checkpointValue.Parent = checkpointData
player.CharacterAdded:connect(function(character)
task.wait()
local storedCheckpoint = ServerStorage.CheckpointData[userIdString].Value
character:MoveTo(storedCheckpoint.Position + Vector3.new(math.random(-4, 4), 4, math.random(-4, 4)))
end)
if checkpointValue.Value ~= checkpoint then
checkpoint.Jingle:Play()
checkpoint.Color = Color3.new(0, 1, 0)
task.delay(1, function()
checkpoint.Color = Color3.new(0.0666667, 0.0666667, 0.0666667)
end)
end
end
checkpointValue.Value = checkpoint
end
end
checkpoint.Touched:Connect(onTouched)