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)
Output:
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)
2 Likes
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
2 Likes
I think is more eazy if I do what @oscoolerreborn said but that works too thank you!
2 Likes
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
1 Like
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)
i would rather not use return end
it just breaks the entire script [i think, idk] i just do
if plr.Character then
--A
end
:0 yes!
ALSO ENABLED NEVER WORKS AAAAAA Try using a frame and changing its “Visible” Property, Trust me aaaa!
return end just breaks out of the parent function (the mousebutton1 event), i do it so there isn’t infinite indents
if player is dead then return end
if player doesnt have enough ammo then return end
if player cant shoot then return end
do stuff
looks cleaner than
if player is alive then
if player has enough ammo then
if player can shoot then
do stuff
end
end
end
you can also do this strategy with loops and break
you can also continue for loops
for i,v in pairs("ScreenGui":GetChildren()) do
if not v:IsA("Frame") then continue end
only frames will go now
end
1 Like
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.)
Edit: Nvm I fixed.
1 Like
Enabled works you need to use PlayerGui to access the ScreenGui and then you can set the Enabled to false/true
1 Like