Can you kill a player through a local script?

Ok, so say i have a randomizer inside a local script, and if the first option was picked, it kills the player. Can this be done inside a local script or do i have to fire an event. If i do which event transfers local to global?

4 Likes

You will definitely need a RemoteEvent here to handle the damaging and if you were to change the Health of another player via a LocalScript, that shouldn’t replicate.

You can take a look at the api-reference and it even shows that it is non-replicated:

4 Likes

Killing the LocalPlayer’s character does work through a LocalScript.

6 Likes

Do NOT host the Randomizer logic in the LocalScript if you’re using a RemoteEvent. Exploiters can just blatantly fire it and kill anyone randomly.

Use the Server to handle all logic and notify your players with it.

6 Likes

Can you show me how/ an example of it?

I suggest you follow Nyapaw’s advice and do some restructuring, but all you would do to kill the LocalPlayer from a LocalScript is

game:GetService("Players").LocalPlayer.Character.Humanoid.Health=0

preferably with some checks for the character and the humanoid.

4 Likes

You cannot kill players’ character other than your own character. Killing them on a client results them dying from client’s perspective while the player itself is still alive.

If there’s a backdoor, the backdoor is capable of bypassing the FilteringEnabled.

3 Likes

You should only kill the player in a regular Script, so to do this you’ll need a RemoteEvent. However, it’s best to handle any math in global scripts. Here’s what some sample code would look like:

--Local Script--
local repStor = game:GetService("ReplicatedStorage")
local killRemote = repStor:WaitForChild("KillRemote") --Requires a RemoteEvent named "KillRemote" to be childed under ReplicatedStorage

killRemote:FireServer() --Sends a message to activate the RemoteEvent

-------------------------------------------

--Global Script--
local repStor = game:GetService("ReplicatedStorage")
local killRemote = repStor:WaitForChild("KillRemote") 

killRemote.OnServerEvent:Connect(function(player) --Receives message, then fires the remaining code
    local x = --Randomoizer
    if x == 1 then
        player.Character.Humanoid.Health = 0
    end
end)

The reason why the “killing” shouldn’t usually happen in the LocalScript is because ideally everyone would want to see you die (unfortunately). Local Scripts should handle stuff that only you (from your own computer) can see, but someone on another computer can’t.

5 Likes

Actually, killing your character from a LocalScript does replicate to the server. That’s why the server will respawn you after doing this.

3 Likes

@GFink
@Super_Shocky
@Operatik
@Nyapaw

MY code
player = game:GetService("Players").LocalPlayer.Character.Humanoid

function kill(player)

player.Health=0

end

local randomizer = math.random(1)

local tool = script.Parent

local event1 = game.ReplicatedStorage.event1

local debounce = false

tool.Activated:Connect(function()

if not debounce then

debounce = true

if randomizer == 1 then kill(player)

wait(1)

debounce = false

end

end

end) ```

any negatives about it? I think imma use a remote event to handle it better, and tthe game will be 1 player servers. And, it’ll be shorter.

Ah, you’re right! I tried it on Studio and the server did see the death after a LocalScript made the character die. It may be a matter of style whether to use Global or Local script, but I do fear that hackers could exploit this, though I may be wrong.

1 Like

It’s a bit hard to read the code, try putting three tick marks ```
behind and in front of your code!

I fixed it check it out. :))))))

Looks good, but beware of where you put the randomizer and how you use it! The line
math.random(1)

only outputs the value 1, though this is fine if you’re just testing the tool for now.

Also, since you put the line

local randomizer = math.random(1)

outside of the Activated function, the variable “randomizer” will only take on 1 value and never change. Thus, the player will either always die or always survive each time they use the tool. This is different from a scenario where the player has a 50% (or other probability) of dying each time the tool is activated. If you want the player to “roll the dice” each time the tool is activated, declare “randomizer” inside the Activated function but before the line
if randomizer == 1 then
I hope this helps!

Yeeah, im just testing it out for the moment, and i changed it up a bit. Mind if i message u privatly for help?

I don’t mind! However, I’m not too familiar with scripting with tools, so I might ask a few questions about them.

Math.random (1,x)

Also, tl;dr. Whole Topic.
Why not just reset the player? Instead of lowering the health. More or less a built in remote to stop hijackers

1 Like

How would you reset the player?

The Player object has a :LoadCharacter() function, this will instantly respawn them without making their humanoid die. However, this function can only be called by the server which may cause problems for you depending on how you want to do this.

Running this code on the client will replicate the client’s death to the server:

game.Players.LocalPlayer.Character.Humanoid.Health = 0

Go ahead and try it, the death of the character replicates to the server just fine.

6 Likes