#Idk how to secure touch event in games.
1 messages · Page 1 of 1 (latest)
Based off of distance should be good enough you can calculate it by subtracting position vectors and getting .Magnitude
would that work for different lengths of weapons?
Yes, you could change the distance for the type of the weapon
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
i also found this other function called GetTouchingParts which registers on server side. Im not sure if u used it before but if u have does it work for touched things?
And make sure to remember to do distance calculations on server
Yes it works with touching things as long as it has a touch interest I believe
if that was your question
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
You absolutely can and latency Id say depends on the type of game
might wanna check this out too
its a pvp game so lots of checking touched but barely any like data saving stuff
yeah ill reference it later
Id say latency isnt that bad depending on how you’re going about it
Best to test and see
got an issue idk how but when I move fast the hit doesnt detect
Can I see your code
** You are now Level 10! **
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}
```
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
Yes don't worry you were right I was just pointing out something else you might wanna watch out for
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
"Im guessing its due to the delay if the character isnt touching touching the handle by the time its on the server"
o okay
can we see the client code as well?
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)
did you check if .Touched is firing?
Can we get a quick sanity check see if it prints 😭 In my experience .touched is kinda unreliable
nice but wait can you put touched in this conditional
i removed the if checked here for server side checks earlier and it works perfectly
is it cuz theres a client and server delay?
yeah thats probably the culprit then 😭
Checking distances on the server should be good enough, or you could do the entire damage/hit calculation on the server
i do the dmg hit calc on server like the serv request the dmg from a sword definition module in server script
only prob is dist ill prob js put a if check on that
I see that but I meant checking collisions on the server rather than .Touched -> double checking collision on the server -> damage
you could do
check collision on the server -> damage
this should work too I think
how would I do that? it would require like cycling through player weapons wouldnt it
wait i dont really get how exploits work like can exploiters require modules and fire client remotes?
yes to both
and wdym cycling through player weapons? you would just have to check collision whenever a weapon is equipped
🤔 wait i could just put a server script under the tool weapons to check collisions that way right
Yepp careful on where you put server scripts tho everything can get disorganized pretty fast