Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

I have 2 questions on this small GUI script?

Asked by 6 years ago

If a player is touching the water part, a blue bar GUI will appear on their screen, showing their air. If they are in for too long, it will start to deal damage until they get out of the water. However, it doesn't show the air bar decreasing, and it also doesn't stop dealing damage if they get out.

local air = script.Parent
local hitbox = game.Workspace.WaterHitbox
local airBarSize = air.CurrentAir.Size.X.Offset
air.Enabled = false

hitbox.Touched:connect(function(part) 
    local humanoid = part.Parent:FindFirstChild("Humanoid") 
    if (humanoid ~= nil) then   --if a humanoid exists
        air.Enabled = true --show air bar
        repeat
            wait (0.01)
            airBarSize = airBarSize - 2
        until airBarSize == 0
        repeat
            humanoid.Health = humanoid.Health - 1
            wait()
        until not airBarSize == 0
    end 
end)

hitbox.TouchEnded:connect(function(part)
    local humanoid = part.Parent:FindFirstChild("Humanoid")
    if (humanoid ~= nil) then
        airBarSize = 544
        air.Enabled = false
    end
end)

2 answers

Log in to vote
1
Answered by
Launderer 343 Moderation Voter
6 years ago

personally i'd add a boolean value in the player somewhere named like "Drowning" or something then inbetween lines 9 and 10 do

humanoid.Parent.Drowning.Value = true

then to fix the endless damage dealing on line 17 do

until not airBarSize == 0 or humanoid.Parent.Drowning.Value == false

then inbetween lines 23 and 24 do

humanoid.Parent.Drowning.Value = false

Now for the airbar not changing or whatever I think it's because you're using a script and like server scripts can't access playergui, idk tho. I might be wrong about that part but i'd still use remotes to deal the damage while the bar shrinking is all client in a localscript.

Ad
Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

This is due to line 3. You’re setting airBarSize to the value of X.Offset at the time you assigned it the variable. For example, if the current value of the Offset was -25, local airBarSize = air.CurrentAir.Size.X.Offset would be the same as local airBarSize = -25. Another thing to add on is that your script will error if you try to assign X, Y, XOffset, or YOffset from a script. These cannot be written from scripts, but manually in the Explorer. You’ll have to use UDim2.new() to set the size.

What I would do for line 3 is simply set it to the object:

local airBar = air.CurrentAir

But you can out this question I asked to get a better understanding of this common mistake:

https://scriptinghelpers.org/questions/61116/when-do-you-store-a-property-as-a-variable

0
Thanks for helping! Shame I can only accept 1 answer. AtomicChocolate 35 — 6y

Answer this question