Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can I player damage in serverscript?

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

Many Thanks, badcarftyt

1 comment

0

I want this to work in an if statement, if pin is incorrect, player damage else print("Pin correct!")

4 answers

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

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

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.

2 comments

0

I want this to work in an if statement, if pin is incorrect, player damage else print("Pin correct!")

0

you can do that inside the RemoteEvent

This discussion is preserved as it was. New answers, comments, and votes are closed.