CheckPoint On The Server Side? NANI!?!?

My problem is I have a checkpoint system on a server script that changes color when its touched. however, because it is not running on each individual client everyone can see the checkpoint’s color change when just one person touches the checkpoint but when I place my script in a local script it breaks and in the toolbox I think almost all free checkpoint for obby models are on a server script so I assume there has to be a reason for that. But how do I make it run on each individual client so that not everyone can see the checkpoint color change when someone else steps on it.

-- Define the starting and ending color and material
local startColor = Color3.new(1, 0.666667, 1)
local endColor = Color3.new(0, 1, 0)
local startMaterial = Enum.Material.SmoothPlastic
local endMaterial = Enum.Material.Neon.Value

local CollectionService = game:GetService("CollectionService")

local CheckPoints = CollectionService:GetTagged("CheckPoints")

-- Define the duration of the color change in seconds
local duration = 1

-- Keep track of the last checkpoint each player touched
local lastCheckpointValues = {}

-- Define the function to tween the part's color and material
local function tweenColor(part)
	-- Create the color and material tweens
	local colorTween = game:GetService("TweenService"):Create(part, TweenInfo.new(duration), {Color = endColor})
	local materialTween = game:GetService("TweenService"):Create(part, TweenInfo.new(duration), {Material = endMaterial})

	-- Play the tweens in parallel
	colorTween:Play()
	materialTween:Play()
end

-- Set up the Touched event for each checkpoint
for i, checkpoint in ipairs(CheckPoints) do
	checkpoint.Touched:Connect(function(hit)
		local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
		if player and player.Character and player.Character:FindFirstChild("Humanoid") then
			-- Disable the last checkpoint the player touched
			local lastCheckpoint = lastCheckpointValues[player.UserId]
			if lastCheckpoint then
				lastCheckpoint.Color = startColor
				lastCheckpoint.Material = startMaterial
			end

			-- Set the new checkpoint as the last checkpoint the player touched
			lastCheckpointValues[player.UserId] = checkpoint

			-- Tween the checkpoint's color and material
			tweenColor(checkpoint)
		end
	end)
end

-- Set each player's respawn location based on the last checkpoint they touched
game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		repeat task.wait() until character.Parent ~= nil

		local lastCheckpoint = lastCheckpointValues[player.UserId]
		if lastCheckpoint and character:FindFirstChild("Humanoid") then
			local spawnPart = lastCheckpoint
			character.HumanoidRootPart.CFrame = spawnPart.CFrame + Vector3.new(0, 0, 0)
		end
	end)
end)

-- Define the starting and ending color and material
local startColor = Color3.new(1, 0.666667, 1)
local endColor = Color3.new(0, 1, 0)
local startMaterial = Enum.Material.SmoothPlastic
local endMaterial = Enum.Material.Neon.Value

local CollectionService = game:GetService("CollectionService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer

local CheckPoints = CollectionService:GetTagged("CheckPoints")

-- Define the duration of the color change in seconds
local duration = 1

-- Keep track of the last checkpoint the player touched
local lastCheckpoint

-- Define the function to tween the part's color and material
local function tweenColor(part)
	-- Create the color and material tweens
	local colorTween = game:GetService("TweenService"):Create(part, TweenInfo.new(duration), {Color = endColor})
	local materialTween = game:GetService("TweenService"):Create(part, TweenInfo.new(duration), {Material = endMaterial})

	-- Play the tweens in parallel
	colorTween:Play()
	materialTween:Play()
end

-- Set up the Touched event for each checkpoint
for i, checkpoint in ipairs(CheckPoints) do
	checkpoint.Touched:Connect(function(hit)
        if not hit then return end

		local player = Players:GetPlayerFromCharacter(hit.Parent)
		if player == Player and player.Character and player.Character:FindFirstChild("Humanoid") then
			if lastCheckpoint then
				lastCheckpoint.Color = startColor
				lastCheckpoint.Material = startMaterial
			end

			-- Set the new checkpoint as the last checkpoint the player touched
			lastCheckpoint = checkpoint

			-- Tween the checkpoint's color and material
			tweenColor(checkpoint)
		end
	end)
end

Player.CharacterAdded:Connect(function(character)
    local HRP = character:WaitForChild("HumanoidRootPart")

    if lastCheckpoint and character:FindFirstChild("Humanoid") then
        local spawnPart = lastCheckpoint
        HRP.CFrame = spawnPart.CFrame + Vector3.new(0, 0, 0)
    end
end)
1 Like

I suggest to do the following instead:

  • Have a client & server script
  • Detect client touched checkpoint on client
    – Change color on touch (LocalPlayer.Character touched not any other character)
    – Fire remote event to server
  • On remote event
    – Check if the distance from the checkpoint is less than … (make sure this number makes count of the player’s height as well as any latency)
    – Save their checkpoint and just do the same

So basically you split up this script into a client and server sided sections.

1 Like

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