I want to change the player humanid health to 0 when he click on the GUI.(I am using one local script and is inside the StarterGui)
local plr = game:GetService("Players").LocalPlayer
local character = plr.Character or plr.CharacterAdded:Wait()
local humanoid = character:FindFirstChild("Humanoid")
local menuGUI = script.Parent.Parent.Parent
script.Parent.MouseButton1Click:Connect(function()
menuGUI.Enabled = false
humanoid.Health = 0
end)
so I’ve had this same problem, it would create a new humanoid when CharAppearance is loaded
try doing
local plr = game:GetService("Players").LocalPlayer
local character = plr.Character or plr.CharacterAdded:Wait()
wait(0.3)
local humanoid = character:FindFirstChild("Humanoid")
local menuGUI = script.Parent.Parent.Parent
script.Parent.MouseButton1Click:Connect(function()
menuGUI.Enabled = false
humanoid.Health = 0
end)
I would do CharacterAppearanceLoaded:Wait() but if the charappearance is already loaded, alternatively you can just get the humanoid inside the function
local plr = game:GetService("Players").LocalPlayer
local character = plr.Character or plr.CharacterAdded:Wait()
local humanoid = character:FindFirstChild("Humanoid")
local menuGUI = script.Parent.Parent.Parent
script.Parent.MouseButton1Click:Connect(function()
if not plr.Character then return end
if not plr.Character:FindFirstChildOfClass("Humanoid") then return end
local humanoid = plr.Character:FindFirstChildOfClass("Humanoid")
menuGUI.Enabled = false
humanoid.Health = 0
end)
Samll tip: Move
local humanoid = character:FindFirstChild(“Humanoid”)
right under
script.Parent.MouseButton1Click:Connect(function()
just try it ill explain it if it works
also read @PostVivic ’ s reply! a its also very helpful
If your going to get the humanoid like how @oscoolerreborn did PLEASE add a check is there is a character. It will save the console from screaming at you
right before humanoid
if not plr.Character then return end
lol here is the code if you need it, implementing @PostVivic 's reply so I don’t make him sad [he wont be sad]:
local plr = game:GetService("Players").LocalPlayer
local character = plr.Character or plr.CharacterAdded:Wait()
local menuGUI = script.Parent.Parent.Parent
script.Parent.MouseClick:Connect(function()
if character:WaitForChild("Humanoid",0.1) then
local Humanoid = plr.Character.Humanoid
menuGUI.Enabled = false
humanoid.Health = 0
end
end)
Just one question. I only can change the humanoid health to 0 one time. I was testing and I turn the menu GUI on for click play again everything happen again but the humanoid health doesnt change again. (When the character is removed from workspace other character is added that can be the issue.)