Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How can I player damage in serverscript?

Asked by 2 years ago

I want the player to get 5hp damage using the serverscript, is there a way it could be done?

Many Thanks, badcarftyt

0
I want this to work in an if statement, if pin is incorrect, player damage else print("Pin correct!") badcraftyt 21 — 2y

4 answers

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

The easiest way is by using

Humanoid:TakeDamage(int)

Also using Humanoid:TakeDamage should be used in serverscripts anyway. Here's more info about it. https://developer.roblox.com/en-us/api-reference/function/Humanoid/TakeDamage NEVER USE HUMANOID:TAKEDAMAGE ON CLIENT SCRIPTS!!

Ad
Log in to vote
0
Answered by 2 years ago

Hello, I don't know if you are trying to make the player take damage via touching a part or just a script. So I'll write a script showing how to damage a player with a part

script.Parent.Touched:Connect(function(hit) -- runs if part is touched
    local humanoid = hit.Parnet:FindFirstChildWhichIsA("Humanoid") -- locates humanoid
    if humanoid then -- checks if the thing that touched the part is a player
        humanoid.Health = humanoid.Health - 5 -- damages the player
    end
end)

This above is a way to damage a player via server script that has a part or object as a parent. Maybe you could do something with Humanoid:TakeDamage()

Thanks for your time. - Zeta

Log in to vote
0
Answered by
Xapelize 2658 Moderation Voter Community Moderator
2 years ago
Edited 2 years ago

TakeDamage is used for damaging, but if a player has ForceField on, it will not damage. Instead, setting the humanoid health directly will not care if player have forcefield on, and damage.

Also they are supposed to do in ServerScripts, example script:

game:GetService("Players").PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        character:WaitForChild("Humanoid").Health -= 5
    end)
end)

(This script makes any joined person damages 5)

oh by the way, heres a script to damage a person when touched a part, but with TakeDamage, so if the person have forcefield, it won't damage

script.Parent.Touched:Connect(function(hit)
    local Humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")

    if Humanoid then
        Humanoid:TakeDamage(5)
    end
end
Log in to vote
0
Answered by 2 years ago

So you will use RemoteEvent if its from a client. I want you to learn how to use it that's why I didn't show the code use the link to understand it.

0
I want this to work in an if statement, if pin is incorrect, player damage else print("Pin correct!") badcraftyt 21 — 2y
0
you can do that inside the RemoteEvent acediamondn123 147 — 2y

Answer this question