So if I killed someone in my game they die on my screen but does not respawn. But on their screen, they see them alive and if they touch me I die and I do not respawn. Please help, thank you.
Player will not die? Please help!
1 answer
Are you killing players in a LocalScript? If so, it makes sense that only you can see it.
ROBLOX uses a Client-Server Model, which means your client can't affect other players. This means that, rather than your computer telling the server "this player is now dead", you need to ask the server "please make this player dead" and then the server will tell everyone "this player is now dead".
However, this is a bad example. The server should not allow the client to automatically kill people arbitrarily. The server should be the one deciding when someone should or shouldn't be killed. In that case, say we have a sword. This sword is supposed to damage people.
In the sword, there is a LocalScript, a Script, and a RemoteEvent. This RemoteEvent will be the 'Hit event'. A RemoteEvent allows a LocalScript to tell a Script on the server that something happened.
In the LocalScript, there will be something like this:
script.Parent.Equipped:Connect(function(mouse)
local connection = script.Parent.Activated:Connect(function()
local target = mouse.Target
if not target then return end
local foundHumanoid = target.Parent
and target.Parent:IsA('Model')
and target.Parent
:FindFirstChildWhichIsA('Humanoid')
if not foundHumanoid then return end
script.Parent.Hit:FireServer(foundHumanoid)
end)
script.Parent.Unequipped:Wait()
connection:Disconnect()
end)
In the Script, there will be something like this (if statement split out into multiple lines for readability purposes):
local debounces = {}
local function isInstance(instance)
return instance
and type(instance) == 'userdata'
and getmetatable(instance)
and instance.ClassName ~= nil
end
script.Parent.Hit.OnServerEvent:Connect(function(player, humanoid)
if not debounces[player]
and isInstance(humanoid)
and humanoid:IsA('Humanoid')
and humanoid.Parent
and humanoid.Parent:IsA('Model')
and humanoid.Parent.PrimaryPart
and player.Character
and player.Character.PrimaryPart
and player.Character.PrimaryPart.CFrame
:ToObjectSpace(humanoid.Parent.PrimaryPart.CFrame)
.Position.Magnitude < 3
then
debounces[player] = true
humanoid:TakeDamage(15)
wait(1)
debounces[player] = false
end
end)
Basically, when you hit a player, the LocalScript is telling the server a player was hit. It's that simple.
Then, the script does all the work. It's very important to make sure the client isn't lying, since you can't trust them EVER. The script makes sure that the player's character exists, the enemy's character/humanoid exists, they are close enough to be hit by the sword, and only allows the player to hurt someone else once per second. This is an incredibly safe way to code. Never believe the client. Always implement server logic on the server.
Anyway, hope I helped. Make sure you fire the event when you swipe the sword, or whatever weapon you have (and make sure you know the Humanoid you're trying to damage), and you should be fine.
Comment if you need any extra help, I can edit my answer, but if I helped remember to upvote and accept~
10 comments
I dont think that many checks is ever neccesary to check if a character exists
You don't know what the client is passing in.
Assuming you do know what the client passes in is one way to invite weird errors/exploits
Anything the client has touched, you need to rigorously test to ensure it's what you want and something you will allow.
first of all, you could just check if the parent of the Humanoid is the player's character and it's a humanoid. I don't really see the need of seeing if the parent of thing sent over has a primary part.
Nor do I see any point in checking the first parameter, as the client doesn't have control over what is sent over as the 1st parameter
but other than that, pretty great explanation
player is the Player object, humanoid is the parameter passed as the client. I don't check that the first parameter is a Player, I only check to make sure they have a character. Meanwhile, I check everything about the second
Passed BY the client, sorry
The PrimaryPart thing is for CFrame to prevent errors.
This discussion is preserved as it was. New answers, comments, and votes are closed.

6 comments
show your code
I do not have a code, Because I do not know where to start, I don't understand what is happening.
simple, filtering enabled
Ok so the problem is that whatever or however you are killing them, you are doing it from the client, so only you see it
Ok so the problem is that whatever or however you are killing them, you are doing it from the client, so only you see it
@DominusInfinitus See my answer