Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Is there some sort of Client-Side .Touched event?

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!

2 comments

0

The event fires on both client and server and can be accessed from both.

0

ok

2 answers

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)

1 comment

0

Doesn't work..?

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.

1 comment

0

I'll try that.

This discussion is preserved as it was. New answers, comments, and votes are closed.