How to reuse this script but for UI

greetings

 local h = part.Parent:findFirstChild("Humanoid")
 if h~=nil then
  h.Health = h.Health-100
 end
end
script.Parent.Touched:connect(onTouched)

i’m not a scripter at all and i was wondering how i should convert this script from dying when touching a part, to being used for a ui button that kills you instead on click

You can make a local script inside of a TextButton or ImageButton and write it so that it gets the Player’s Character and waits for a child named Humanoid, then sets the Humanoid’s Health property to 0 when it’s found.
EDIT: Forgot to get Character before getting Humanoid.

Example code:

local Button = script.Parent

Button.Activated:Connect(function()
	game.Players.LocalPlayer.Character:WaitForChild("Humanoid").Health = 0
end)
local Players = game:GetService("Players")

script.Parent.MouseButton1Click:Connect(function()
	local plr = Players.LocalPlayer
	local chr = plr.Character
	if chr then
		local hm = chr:FindFirstChild("Humanoid")
		if hm then
			hm.Health = 0
		end
	end
end)

you forgot to get the character before the humanoid

Fixed it. Thanks for telling me.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.