onTouch and Cloning problems

I’ve been trying to make it where when a player touches a part a timer will start on there screen.

A timer (which is a GUI) is cloned for every player on joining. Then when a player touches a certain part that timer will start ticking down for that player. The problem is that when a player touches a part, the timer doesn’t start for that player but instead starts for everybody no matter who touches the part.

The script below is currently in a local script in StarterPlayerScripts -

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

-- Cloned Guis

for _, player in pairs(game:GetService("Players"):GetPlayers()) do
	local clonedgui = game.ReplicatedStorage.Timer:Clone()
	clonedgui.Parent = player.PlayerGui
end

-- Functions

local function teleport()
	local clonedgui = Player.PlayerGui:WaitForChild("Timer")
	local minutes = 1
	local seconds = 0
	repeat
		if seconds <= 0 then
			minutes = minutes - 1
			seconds = 59
		else
			seconds = seconds - 1
		end
		if seconds < 10 then
			clonedgui.TextLabel.Text = tostring(minutes)..":0"..tostring(seconds)
		else 
			clonedgui.TextLabel.Text = tostring(minutes)..":"..tostring(seconds)
		end
		Player.Character.Humanoid.Died:Connect(function()
			wait(5)
			minutes = 1
			seconds = 0
		end)
		wait(1)
	until minutes <= 0 and seconds <= 0
end

local debounce = false

Checkpoint3.Touched:Connect(function(touched)
	if touched.Parent:IsA("Model") and touched.Parent:FindFirstChild("Humanoid") then
		local Player = Players:GetPlayerFromCharacter(touched.Parent)
		if Player then
			if not debounce then
				debounce = true
				teleport()
				wait(1)
				debounce = false
			end
		end
	end
end)

I’m not sure what went wrong and would appreciate some help, thanks.

You Should use Remote Event in a ServerScript like this:

--Script in ServerScriptService
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
 
local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEventTest")
 
local function onPlayerAdded(player)
	-- Fires the remote event
	remoteEvent:FireClient(player)
end
Players.PlayerAdded:Connect(onPlayerAdded)

--LocalScript 

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEventTest") 

--stuff

remoteEvent.OnClientEvent:Connect(Yourfunction)
1 Like

It still doesn’t seem to be working. I want it where when a player steps on a part, the timer will start on that players screen.

Here is what I have -

-- Script in ServerScriptService
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEventTest")

local function onPlayerAdded(player)
	remoteEvent:FireClient(player)
end
Players.PlayerAdded:Connect(onPlayerAdded)

-- Local Script in StarterPlayerScripts
local Players = game:GetService("Players")
local Checkpoint3 = game.Workspace.Checkpoint3
local Player = Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEventTest") 

local function Timer()
local clonedgui = game.ReplicatedStorage.Timer:Clone()
	clonedgui.Parent = Player.PlayerGui
end

remoteEvent.OnClientEvent:Connect(Timer)

local function teleport()
	local clonedgui = Player.PlayerGui:WaitForChild("Timer")
	local minutes = 1
	local seconds = 0
	repeat
		if seconds <= 0 then
			minutes = minutes - 1
			seconds = 59
		else
			seconds = seconds - 1
		end
		if seconds < 10 then
			clonedgui.TextLabel.Text = tostring(minutes)..":0"..tostring(seconds)
		else 
			clonedgui.TextLabel.Text = tostring(minutes)..":"..tostring(seconds)
		end
		Player.Character.Humanoid.Died:Connect(function()
			wait(5)
			minutes = 1
			seconds = 0
		end)
		wait(1)
	until minutes <= 0 and seconds <= 0
end

local debounce = false

Checkpoint3.Touched:Connect(function(touched)
	if touched.Parent:IsA("Model") and touched.Parent:FindFirstChild("Humanoid") then
		local Player = Players:GetPlayerFromCharacter(touched.Parent)
		if Player then
			if not debounce then
				debounce = true
				teleport()
				wait(1)
				debounce = false
			end
		end
	end
end)

Put this in a Script (serverside) parented to Checkpoint3 and use RemoteEvents/functions to fire the Teleport() function

1 Like