I was creating some levels, in which you have to touch a brick to teleport to the level area.
So the ServerScript
inside of the Part
went something like this:
local canRecieve = true script.Parent.Touched:Connect(function(hit) if hit.Parent:WaitForChild('Humanoid') then local player = game.Players:GetPlayerFromCharacter(hit.Parent) if canRecieve == true then canRecieve = false player:WaitForChild('leaderstats').Vertigo.Value = player:WaitForChild('leaderstats').Vertigo.Value + 1000 game.ReplicatedStorage.OneThousandRecieved:FireClient(player) hit.Parent:WaitForChild('HumanoidRootPart').CFrame = CFrame.new(40.882, 3.974, -9.194) wait(60) canRecieve = true end end end)
The thing is, it's server-side. I want the cooldown of the .Touched
event ONLY for the client, so that no one can abuse this system and not let anyone through.
Do I have to use RemoteEvents
? If so, how could I use it?
Help please!
You can use Local Scripts for Touched Events. But Local Scripts do not work in Workspace unless it is a descendant of a Player's Character Model. So you would have to store the Local Script into StarterPlayerScripts.
Since we are working on a Local Script. We want to make sure the player who touched the part is the local player so we can keep it all local.
-- Local Script -- -- Variables local StarterPlayerScripts = script.Parent local part = workspace.Part -- Functions local function onTouch(hit) local players = game:GetService("Players") local player = players:GetPlayerFromCharacter(hit.Parent) local localPlayer = players.LocalPlayer if player and player == localPlayer then print(player) end end -- Events/Connections part.Touched:Connect(onTouch)
Adding leaderstats on the client side = bad idea.
What I would do is create an array like this:
local deb = {} script.Parent.Touched:Connect(function(hit) if hit.Parent:WaitForChild('Humanoid') then local player = game.Players:GetPlayerFromCharacter(hit.Parent) if (deb[hit.Parent.Name] == true) then return end deb[hit.Parent.Name] = true player:WaitForChild('leaderstats').Vertigo.Value = player:WaitForChild('leaderstats').Vertigo.Value + 1000 game.ReplicatedStorage.OneThousandRecieved:FireClient(player) hit.Parent:WaitForChild('HumanoidRootPart').CFrame = CFrame.new(40.882, 3.974, -9.194) wait(60) deb[hit.Parent.Name] = false end end)
This stores an individual debounce (what you're attempting to achieve) value for every player. Remember that filtering enabled causes no data to be "written" (you could call it) from the client. Therefore, you cannot set leaderstats for everyone else unless its server side.