#Idk how to secure touch event in games.

1 messages · Page 1 of 1 (latest)

glacial halo
#

I have a touched event for my sword and it fires a hit remote to server with the victims name and the sword he is holding to register damage on server. But the thing is wont exploiters just fire the remote on any player they want, like hitting someone far away?

vale frost
glacial halo
vale frost
#

Id suggest testing it out a bit seeing what works and what doesnt and might wanna not make it too strict it might become unplayable with lag

glacial halo
vale frost
#

And make sure to remember to do distance calculations on server

vale frost
#

if that was your question

glacial halo
#

cant I get the handle of the sword which is a part and see if the touched parts contain victim humanoid on serverside if yes then register the dmg

#

or would latency mess that up

vale frost
#

You absolutely can and latency Id say depends on the type of game

#

might wanna check this out too

glacial halo
glacial halo
vale frost
#

Best to test and see

glacial halo
vale frost
glacial halo
high locustBOT
#

studio** You are now Level 10! **studio

vale frost
# glacial halo

You might wanna be careful with this part, it might be buggy with delay if the player switches to a different tool or one that cannot deal damage

Im guessing its due to the delay if the character isnt touching touching the handle by the time its on the server

#

Can I see your client code as well?

also it might be helpful to format the code like this so its easier to share and edit on discord

```lua
{code}
```

glacial halo
#

sure but the delay has to do with the middle part cuz print ("3) wont print unless im slow. the first two prints really fast within touch detection

vale frost
glacial halo
#
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Combat = require(ReplicatedStorage:WaitForChild("Modules"):WaitForChild("Combat"))

local hitEvent = ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("SwordHit")

hitEvent.OnServerEvent:Connect(function(player, remoteVictim)
    --Check
    local character = player.Character
    if not character then return end
    local tool = character:FindFirstChildOfClass("Tool")
    if not tool then return end
    local handle = tool:FindFirstChild("Handle")
    if not handle then return end
    print("check")
    --Validate
    local overlappingParts = workspace:GetPartsInPart(handle)
    local validatedHit = false
    print(overlappingParts)
    
    for _, part in ipairs(overlappingParts) do
        local victimChar = part:FindFirstAncestorOfClass("Model")
        print(victimChar, remoteVictim)
        if victimChar and victimChar:FindFirstChild("Humanoid") and victimChar == remoteVictim then
            print("FINAL")
            validatedHit = true
            break
        end
    end

    if validatedHit then
        print("3")
        Combat.ApplyDamage(character, remoteVictim, tool.Name)
    end
end)


i print a print("Final") inside the if statement and its the part thats delayed

vale frost
vale frost
glacial halo
#
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local player = Players.LocalPlayer
local sword = script.Parent
local handle = sword:WaitForChild("Handle")

local hitEvent = ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("SwordHit")

handle.Touched:Connect(function(hit)
    --print(handle.Velocity.Magnitude)
    local character = player.Character
    if not character then return end
    if hit:IsDescendantOf(character) then return end 

    local victim = hit.Parent
    if victim:FindFirstChild("Humanoid") then
        hitEvent:FireServer(victim, sword.Name)
    end
end)

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local player = Players.LocalPlayer
local sword = script.Parent

sword.Equipped:Connect(function(mouse)
    local handle = sword:WaitForChild("Handle")
    local lastPosition = handle.Position

    RunService.Heartbeat:Connect(function(dt)
        local currentPosition = handle.Position
        local delta = currentPosition - lastPosition
        local speed = delta.Magnitude / dt  
        lastPosition = currentPosition
    end)
end)

vale frost
glacial halo
#

yeah .touched should be firing fine

#

already checked

vale frost
#

Can we get a quick sanity check see if it prints 😭 In my experience .touched is kinda unreliable

glacial halo
#

sure

vale frost
glacial halo
#

i removed the if checked here for server side checks earlier and it works perfectlycrying is it cuz theres a client and server delay?

vale frost
#

Checking distances on the server should be good enough, or you could do the entire damage/hit calculation on the server

glacial halo
#

only prob is dist ill prob js put a if check on that

vale frost
vale frost
glacial halo
#

wait i dont really get how exploits work like can exploiters require modules and fire client remotes?

vale frost
glacial halo
vale frost