Hey! I wanted some feedback on my code for a game I’m developing. Simply speaking the aim is to detect if a player touches/collides with another player, which in turn sends a request to the server to carry out some actions.
Currently it does work, however it takes a second for the touch to be detected sometimes, and you really have to be up against the other player’s character, or be running into them. If this isn’t the best way to handle it as shown below, any suggestions as to how I could improve it would be highly appreciated. The goal is to have an efficient system where you can collide with other players and have it do something server-side.
Here’s the code in it’s current state. EDIT Edited to comply with suggestions in the post below.
local touchedConnection = Humanoid.Touched:Connect(function(Hit)
if not Touched then
Touched = true
Touched = false
local HitPlayer = Players:GetPlayerFromCharacter(Hit.Parent) or Players:GetPlayerFromCharacter(Hit.Parent.Parent)
if HitPlayer then
local HitPlayerHumanoid = HitPlayer.Character.Humanoid
if HitPlayerHumanoid.Health == 0 then return end
if (LocalPlayer.TeamColor == BrickColor.new('Pastel Blue') and HitPlayer.TeamColor == BrickColor.new('Persimmon')) then
-- fire RemoteEvent
elseif (LocalPlayer.TeamColor == BrickColor.new('Persimmon') and HitPlayer.TeamColor == BrickColor.new('Persimmon')) then
-- fire RemoteEvent
if touchedConnection then
I would replace this with
You do this for both teams, I would put this statement above the team checking if statements.
This might be caused by your
Touched debounce. Consider maybe using a table of parts on cool down instead or just entirely removing the debounce. The client running Touched events shouldn’t be too intensive anyway.
The last thing I would note, is that this could be exploited fairly easily. I could teleport myself to every player and automatically win. I could also use a ‘lagswitch’ (disconnecting my internet) to touch other players and they would be completely helpless to escape me.
Edit: Removed the
Humanoid.Died comment, I understand why you’re using it now.
Thanks for the feedback, I’ll take it onboard and modify the code accordingly.
Yeah I was worried about that. How should I combat this? I will add exploit prevention for players teleporting and for walkspeed/noclip exploits. Isn’t lagswitching patched now? I’m pretty sure it does kicks you from the game.
Edit: Also on top of this I realise that exploiters can just fire my RemoteEvent aswell and freeze anyone they want. What can I do to fix this?
Haven’t heard much about it, regardless using this script on the client could give clients who have a lot of network latency a huge advantage.
Exploit protection against Touched events is always a fun story. Certainly, as you mention, work on preventing teleports/speed hacks/noclip. Unfortunately, you also have to deal with things such as a client resizing a part so that the Touched event hits everyone (I believe jailbreak had this problem, where exploiters were resizing the lasers causing everyone to die). You could combat this by checking the magnitude between players to determine if the Touch was reasonably valid.
Yeah, I had to deal with this issue in a gunfighting game for a group. It’s referred to (incorrectly) as ‘Hitbox Expanding’ in the community where players were able to insert an Accessory into other players, resize it like crazy and then just shoot it, which would damage the player.