Still have questions? Join our Discord server and get real time help.
Log in to vote

Player will not die? Please help!

Asked by 6 months ago

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.

show your code BashCaster 2324 — 6mo
I do not have a code, Because I do not know where to start, I don't understand what is happening. Leqtrix 7 — 6mo
simple, filtering enabled theking48989987 2009 — 6mo
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 RetroGalacticGamer 232 — 6mo
View all comments (2 more)
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 RetroGalacticGamer 232 — 6mo
@DominusInfinitus See my answer TerrodactyI 158 — 6mo

1 answer

Log in to vote
Answered by 6 months ago
Edited 6 months ago

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:

    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

        if not foundHumanoid then return 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

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
        .Position.Magnitude < 3
        debounces[player] = true


        debounces[player] = false

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~

I dont think that many checks is ever neccesary to check if a character exists theking48989987 2009 — 6mo
You don't know what the client is passing in. TerrodactyI 158 — 6mo
Assuming you do know what the client passes in is one way to invite weird errors/exploits TerrodactyI 158 — 6mo
Anything the client has touched, you need to rigorously test to ensure it's what you want and something you will allow. TerrodactyI 158 — 6mo
View all comments (6 more)
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. theking48989987 2009 — 6mo
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 theking48989987 2009 — 6mo
but other than that, pretty great explanation theking48989987 2009 — 6mo
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 TerrodactyI 158 — 6mo
Passed BY the client, sorry TerrodactyI 158 — 6mo
The PrimaryPart thing is for CFrame to prevent errors. TerrodactyI 158 — 6mo

Answer this question