You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I want to make the script do something once the humanoid.health is under 30
What is the issue? Include screenshots / videos if possible!
basically I have a humanoid.healthchanged in a local script then I fire a remote event, and on the server I want to add an revive button once someone goes under 30 health, but what I did to check was this:
if humanoid.Health < 30 then
print("health under 30")
--some more code
if humanoid.Health >= 30 then
print("Health = 30+")
--some more code
Even tho my health is 15 it keeps printing “Health = 30+”. I can add more code if needed and a quick note is that the player has 130 health. Please help!
local rep = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer
local character = player.Character
local rem = rep.Events:WaitForChild("Revive")
local humanoid = character:FindFirstChildWhichIsA("Humanoid")
humanoid.HealthChanged:Connect(function()
if humanoid.Health < 30 then
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
rem:FireServer()
print("Fired to server")
wait(0.1)
local revbutton = character.Torso.ReviveGui.RevButton
revbutton.Visible = false
end
if humanoid.Health >= 30 then
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)
rem:FireServer()
end
end)
This is the server script
local rep = game:GetService("ReplicatedStorage")
local remote = rep.Events.Revive
remote.OnServerEvent:Connect(function(player)
print("Fired On Server")
local character = player.Character
local humanoid = character:FindFirstChildWhichIsA("Humanoid")
if humanoid.Health < 30 then
print("health under 30")
humanoid.WalkSpeed = 5
print("Trying animations")
character.Animate.walk.WalkAnim.AnimationId = "rbxassetid://7229426734"
character.Animate.run.RunAnim.AnimationId = "rbxassetid://7229426734"
character.Animate.idle.Animation1.AnimationId = "rbxassetid://7229422044"
print("Done animations")
local revivegui = rep.BillboardGuis:WaitForChild("ReviveGui"):Clone()
revivegui.Parent = character.Torso
local revivebutton = revivegui.RevButton
revivebutton.Text = "Revive"
revivebutton.MouseButton1Click:Connect(function()
wait(3)
revivebutton.Text = "Reviving"
humanoid.Health = 30
revivebutton:Destroy()
end)
end
if humanoid.Health >= 30 then
print("Health = 30+")
local revivegui = character:FindFirstChild("ReviveGui")
if revivegui then
revivegui:Destroy()
end
humanoid.WalkSpeed = 16
character.Animate.walk.WalkAnim.AnimationId = "rbxassetid://7228751837"
character.Animate.run.RunAnim.AnimationId = "rbxassetid://7228750509"
character.Animate.idle.Animation1.AnimationId = "rbxassetid://0"
end
end)
I hate to tell you but that is some nightmare fuel for code and honestly a really bad response. Firstly, you’re indefinitely running this script with no timeout, which will exhaust the script. Secondly you’re polling when there’s an Event which does exactly that but better, which is what they’re using.
Lastly, instead of using elseifs you just started another if statement, and didn’t even provide it an end so luckily this script wont break the game since it wont run in the first place due to the syntax error.
HealthChanged provides a parameter which is the new health, so just instead of doing humanoid.Health store the parameter as something like humHealth and then do if humHealth > val then.
no no I have a seperate script to make the clients hp 130 when joining (in server script service) I use a local script only to make the hotbar invisible and send a remote