Hello! I am a new builder and one thing has been nagging at me for a while. When I use any weapon in my game to kill players, their health goes to 0, and it is visible on the health bar, however, they do not die and just continue as if they had not been damaged at all.
I have tried very hard, but had no success in scripting this. I want a script where when a players health = 0, they respawn back to the spawn point. I am sorry I do not have any code, since I am a new scripter and hopefull to learn.
Thankyou!
(This answer only applies if you have FilteringEnabled on; you can check it by looking in the Properties window with the Workspace selected.)
The default behaviour is for the player to die when their health reaches 0. Assuming you haven't changed that (and haven't changed the health system and haven't changed the player's default model), then what you're describing is what one would expect if your weapons you're using were not designed for FilteringEnabled -- that is, they attempt to hurt the player's Health directly. This causes their health drain to be only visible to you. To fix this, you have to have the server run the health drain code instead of the client. A very insecure method is to have a RemoteEvent that lets any client tell the server who they hit and how much damage they caused. Do note that scripting it like this is the easiest route, with the drawback that any exploiter will be able to kill everyone else in the server instantly and repeatedly (though you can add checks to limit this, if you know how). An even more insecure method is to disable FilteringEnabled (granting exploiters free reign), but at least your game would be playable if you just invite a few trusted friends into it.
Edit: Here's the insecure method. In every weapon, find where it damages the player and instead fire a RemoteEvent that you have placed in game.ReplicatedStorage named "DamagePlayer"
--In every weapon, instead of hum:TakeDamage(5) --or instead of hum.Health = hum.Health - 5 --do: game.ReplicatedStorage.DamagePlayer:FireServer(hum, 5) --In a Script in ServerScriptService or Workspace: game.ReplicatedStorage.DamagePlayer.OnServerEvent:Connect(function(player, humanoid, damage) if not humanoid then return end -- Make sure the humanoid (still) exists if type(damage) ~= "number" or damage < 0 or damage > 100 then return end -- This makes sure that, if we continue on in the function, 'damage' is a reasonable value. If it isn't, you either have a bug or an exploiter. NOTE: If you can put the maximum damage to below 100 (ex if all your weapons do no more than 20 damage), then change the '> 100' to '> 20' (or whatever value you need) to reduce the amount of damage an exploiter can do. --TODO Check to make sure that 'player' can attack 'humanoid'. ex, have they made a lot of attacks recently (more than is possible for a player)? Is the 'player' anywhere near 'humanoid' (but be careful checking that if you have ranged weapons - ex, you expect a sniper gun to do damage from far away!)? The better you can make this check, the less likely an exploiter can instant-kill everyone. --Assuming all is well: humanoid:TakeDamage(damage) end)
A superior method (requires more scripting skills) is the client telling the server the weapon used and the angle it was fired at; the server can then determine if any damage should be applied to anyone. Note that this is still weak to aim-bots, but at least exploiters wouldn't be able to kill people through walls.