Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago

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!

0
The event fires on both client and server and can be accessed from both. MrLonely1221 701 — 4y
0
ok Sensei_Developer 298 — 4y

2 answers

Log in to vote
1
Answered by
ryan32t 306 Moderation Voter
4 years ago
Edited 4 years ago

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)
0
Doesn't work..? Sensei_Developer 298 — 4y
Ad
Log in to vote
1
Answered by
5_4h 8
4 years ago

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.

0
I'll try that. Sensei_Developer 298 — 4y

Answer this question