Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

Regenerate Health Gui not working?

Asked by 2 months ago

so I have to ask this question a second time since nobody answered my last one

so I made a health GUI and it works perfectly fine but there is one slight problem it wont regenerate every 3 seconds like add 1 health every 3 seconds or something and it still wont work

wait(2)
game.Players.LocalPlayer.Character.Humanoid.Changed:connect(function()
local redzone = game.Players.LocalPlayer.Character.Humanoid.MaxHealth / 4
local yellowzone = game.Players.LocalPlayer.Character.Humanoid.MaxHealth / 2
if game.Players.LocalPlayer.Character.Humanoid.Health <= yellowzone then
    script.Parent.BackgroundColor3 = Color3.new(255,255,0)
end
if game.Players.LocalPlayer.Character.Humanoid.Health > yellowzone then
local brickcolor = BrickColor.new("Lime green")
local color = brickcolor.Color
script.Parent.BackgroundColor3 = color
end
if game.Players.LocalPlayer.Character.Humanoid.Health <= redzone then
script.Parent.BackgroundColor3 = Color3.new(255,0,0)
end
if game.Players.LocalPlayer.Character.Humanoid.Health > redzone then
local brickcolor = BrickColor.new("Lime green")
local color = brickcolor.Color
script.Parent.BackgroundColor3 = color
end
script.Parent.Size = UDim2.new(game.Players.LocalPlayer.Character.Humanoid.Health / game.Players.LocalPlayer.Character.Humanoid.MaxHealth,0,0,20)

end)
0
set a variable for the players service, localplayer, character, humanoid, and health please, also indent SinisterMemories 688 — 2mo

1 answer

Log in to vote
0
Answered by
Isaque232 171
2 months ago
Edited 2 months ago

Hello, please make sure to indent your code, you also should create variables as they make things a lot easier and really prevents some issues from happening with your code, It seems also that the code:

if game.Players.LocalPlayer.Character.Humanoid.Health > yellowzone then
    local brickcolor = BrickColor.new("Lime green")
    local color = brickcolor.Color
    script.Parent.BackgroundColor3 = color
end

is not needed as if Humanoid.Health is already higher than the redzone It'll already change to Lime green.

I've modified your code in a way that it works with the GUI I've tested which should work for yours too, Once again I suggest to make variables as they're not evil and they're also really helpful!

In order to regenerate the player's health you'll need a remoteevent since you cannot do it from a localscript, in this case I created a remoteevent called " RegenHealth " in workspace which will be used to regenerate the player's health.

Modified localscript:

-- Try to put variables as they're really helpful!
local LocalPlayer = game:GetService("Players").LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:wait()
local Humanoid = Character:WaitForChild("Humanoid")
local RegenEvent = game.workspace.RegenHealth

Humanoid.Changed:Connect(function()

    local redzone = (Humanoid.MaxHealth / 4)
    local yellowzone = (Humanoid.MaxHealth /2)

    if Humanoid.Health <= redzone then -- If Humanoid.Health is lower or equal than redzone
        script.Parent.BackgroundColor3 = Color3.new(255,0,0)
    end

    if Humanoid.Health > redzone then -- If Humanoid.Health is higher than redzone
        local color = BrickColor.new("Lime green").Color
        script.Parent.BackgroundColor3 = color
    end

    script.Parent.Size = UDim2.new(Humanoid.Health / Humanoid.MaxHealth,0,0,20) -- Changes the size of the gui with the Health and MaxHealth the player currently has.
end)

while wait(1) do -- Change 1 to the seconds you want it to regenerate
    RegenEvent:FireServer(1) -- Change the 1 to how much you want it to regenerate
end

ServerScript ( Inside remoteevent )

script.Parent.OnServerEvent:Connect(function(Player, HealthAmount)
    local Character = Player.Character or Player.CharacterAdded:Wait()
    local Humanoid = Character:WaitForChild("Humanoid")

    Humanoid.Health = Humanoid.Health + HealthAmount
end)

Hopefully this solves your issue! Make sure to accept the answer if it did.

0
Ok so should I copy and paste that after my original code or should I delete all of my original code and just copy and paste that? THEGOBLINTOPLAD 0 — 2mo
0
@Isaque232 oK sO I tried my comments advice and the health still won't regenerate thanks for making my code more shorter and less stuffy though! THEGOBLINTOPLAD 0 — 2mo
0
You should delete the original code and paste that code instead, and oh sorry, I completly forgot to add the regeneration part, I'll make sure to edit the post with it included! Sorry for that :P Isaque232 171 — 2mo
0
Ok so now the green health bar won't move THEGOBLINTOPLAD 0 — 2mo
View all comments (18 more)
0
Alright, Edited my answer, Check it out now and see if it works! Isaque232 171 — 2mo
0
@Isaque232 Like it is regenerating which is perfect but now the green part of my GUI won't move or decrease (they're called "frame") THEGOBLINTOPLAD 0 — 2mo
0
Just tested my script again and it worked perfectly fine for me, Are you sure you modified it correclty? Make sure that It's a localscript and It's parent is the frame! Isaque232 171 — 2mo
0
@Isaque232 sorry if I sound simple but what do you mean by "remoteevent"? and "serverscript"? where do I put those? all I see is a serverscriptservice and if I put the serverscript in there will it effect everyone in the game? THEGOBLINTOPLAD 0 — 2mo
0
yes the frame is the parent of the localscript and what am I supposed to modify exactly? THEGOBLINTOPLAD 0 — 2mo
0
What I mean by Serverscript is a regular script ( not a localscript ), You can also read more about RemoteEvents here: https://developer.roblox.com/api-reference/class/RemoteEvent they can be added using the default " Insert object " function which appears when you right click in a object. Isaque232 171 — 2mo
0
Should I put the serverscript in the frame where the localscript is? THEGOBLINTOPLAD 0 — 2mo
0
You can put the serverscript inside the RemoteEvent you did create. Isaque232 171 — 2mo
0
I don't remember creating any remote event, and if I am going to where do I out that in the frame thats parent to the localscript? THEGOBLINTOPLAD 0 — 2mo
0
Put the remoteevent in workspace, you can use the default " insert object " button ( Right click in workspace go down until you find the Insert object function and RemoteEvent should be on the list ) Isaque232 171 — 2mo
0
You can also put this on your studio console: local RegenEvent = Instance.new("RemoteEvent") RegenEvent.Parent = workspace RegenEvent.Name = "RegenHealth" to create a RemoteEvent but the other way might be easier. Isaque232 171 — 2mo
0
No it still hasn't worked the weird thing is it used to move when I didn't have the remoteevent or the regen script THEGOBLINTOPLAD 0 — 2mo
0
Are you sure you did put the RemoteEvent in workspace? It's name is: RegenHealth? Your regular script is inside the RemoteEvent? Isaque232 171 — 2mo
0
Ok sorry I just forgot to name it to RegenHealth and it can move now but its literally not regenerating anymore THEGOBLINTOPLAD 0 — 2mo
0
Make sure that the regular script that is inside the remoteevent is exactly the one that is in my post, if it is then it should be working alright, just to make sure also see if your localscript is also correctly modified. Isaque232 171 — 2mo
0
Okay so the local script is completely alike yours aswell as the serverscript yet it still isn't regenerating THEGOBLINTOPLAD 0 — 2mo
0
Just copy pasted both of my scripts on my post to where should be and it worked perfectly fine again, there must be something wrong. Are you sure you did everything alright? Also do you have discord it'd be faster to solve this if we did talk from there. Isaque232 171 — 2mo
0
my discord is WiredExpiration#7224 THEGOBLINTOPLAD 0 — 2mo
Ad

Answer this question