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

How to I make this set to the correct value (0)?

Asked by 5 years ago
Edited 5 years ago

I improved on the code from earlier today (it's not perfect) and now it's acting weird. When I touch all the bricks (meaning the "Health" should set to zero) it does not set to zero. I'm sure it's in my math somewhere but I cannot find where or how

Instead, it sets to 36.61%: Photo

My code (Please answer my question if you can, not ridiculing on how my code can be improved without explaining how :FrowningFace: )

local debounce = false
local house_1 = workspace:WaitForChild("House")
local house_1_pieces = house_1:WaitForChild("Building")
local house_1_health = house_1:WaitForChild("Config"):WaitForChild("Health")


for _, parts in pairs(house_1_pieces:GetChildren()) do
    parts.Touched:Connect(function(plr)
        if plr.Parent:FindFirstChild("Humanoid") ~= nil then
            if parts:FindFirstChild("wasTouched").Value ~= true then
            if debounce ~= true then
                debounce = true
                local unRounded = house_1_health.Value - (house_1_health.Value/#house_1_pieces:GetChildren())
                house_1_health.Value =  math.floor(((unRounded*100)+0.5))/100
                parts.wasTouched.Value = true           
                local start = Color3.new(1,1,1)
                local End = Color3.new(1,0,0)
                parts.Color = Color3.fromRGB(255, 255, 255)
                parts.Anchored = false
                parts.Material = Enum.Material.Neon
                wait(0.1)
                debounce = false
                wait(1)
                parts.Color = Color3.fromRGB(255, 0,0)
            end         
        end

    end
    end)
end

Inside the "Health" BillboardGui:

script.Parent.Text = "Health: 100%"
workspace:WaitForChild("House").Config.Health.Changed:Connect(function()
    script.Parent.Text = "Health: "..workspace:WaitForChild("House").Config.Health.Value.."%"
end)
0
its in line 13 somehow mattchew1010 396 — 5y
0
That's what I thought, but I cant figure out how TheGreenSuperman 85 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

If I am understanding the problem correctly, you are trying to lower the "health" of a building every time one of its children is touched (Excluding repeated bricks), and the health % is based on the amount of untouched bricks remaining. If this is the case, then your error is on line 13.

In order to solve this you need to define what the max health of the object is. The reason for this is that you are currently dividing the CURRENT health of the object by the number of parts in the object.

Here is an example of why this is problematic. If I have a building whose health is 100 and has 5 parts inside of it, ideally the health goes down by 20 every time a brick is touched (100 - 5 * 20 = 0).

Right now, according to your calculations the results would be:

local health = 100;

health = 100 - (100/5) -- 80
health = 80 - (80/5) -- 64
health = 64 - (64/5) -- 51.2
health = 51.2 - (51.2/5) -- 40.96
health = 40.96 - (40.96/5) -- 32.768, final health after all objects touched

See the problem? you are not subtracting the correct amount.

To solve this, I would suggest adding another config value called MaxHealth that stores the object's max health. This way you can get the correct value you are supposed to be subtracting by.

Once you have done that line 13 should end up looking something like this:

local unRounded = house_1_health.Value - (house_1_MaxHealth.Value/#house_1_pieces:GetChildren())

Hope this helps. Let me know if I misunderstood the problem or something doesn't work.

0
Now it's ending at 0.25% :/ TheGreenSuperman 85 — 5y
0
It might be happening because you are saving the rounded value of the health. Try setting the health value to unRounded, and then round it in the script that displays the health. GrandpaScriptin 148 — 5y
0
That worked. Thanks for the lengthy explanation and answer! TheGreenSuperman 85 — 5y
0
Glad I could help :) GrandpaScriptin 148 — 5y
Ad

Answer this question