I did do my research before coming to post here, but I couldn’t find the solution. I’m currently making a mines system where you can break crystals. If the HP (IntValue) of the crystal hits 0 it should disappear. But for some reason the script can’t detect if the HP hit 0.
--CrystalBreaker script
local HP = script.Parent
while HP.Value == 0 do
print("hp is 0")
script.Parent.Parent.Parent.Crystal1.Cylinder.Transparency = 1
script.Parent.Parent.Parent.Crystal1.Cylinder.CanCollide = false
script.Parent.Parent.Parent.Crystal1.Cylinder2.Transparency = 1
script.Parent.Parent.Parent.Crystal1.Cylinder2.CanCollide = false
print("set everything to invisble")
end
Can you please show how a crystal “takes damage”?
It could happen if the health of the crystal doesn’t get to exactly 0 but -1 for example, so I would recommend changing the HP.Value == 0 to HP.Value <= 0
A while loop executes a piece of code as long as the condition is true. In your code
This basically means “execute that piece of code as long as the hp value is 0”. So, when it runs that, it checks if hp value is 0. It finds that it isn’t (it wasn’t broken yet) so it doesn’t even execute the loop a single time. That’s why when the hp value does reach zero, it isn’t checking if it is and your code in the loop doesn’t run.
In order to fix this issue, consider replacing the while loop with a .Changed event connection, which executes the code each time the value is changed. It should look like this:
local HP = script.Parent
HP.Changed:Connect(function()
if HP.Value <= 0 do
print("hp is 0")
script.Parent.Parent.Parent.Crystal1.Cylinder.Transparency = 1
script.Parent.Parent.Parent.Crystal1.Cylinder.CanCollide = false
script.Parent.Parent.Parent.Crystal1.Cylinder2.Transparency = 1
script.Parent.Parent.Parent.Crystal1.Cylinder2.CanCollide = false
print("set everything to invisble")
end
end)
(And yes, what @WoTrox said is valid, that’s why I included it here)
I’m using ZonePlus to detect if the player is near the crystal.
And when the player enters the zone it makes a textbutton visible which fits the whole screen and if I click that button it removes the HP.
--Crystal Script
local playersInZone = require(game.ServerScriptService.ZoneModuleScripts.Crystals.Crystal1)
local Zone = require(game.ReplicatedStorage.Zones.Crystals.Crystal1)
local container = script.Parent
local zone = Zone.new(container)
zone.playerEntered:Connect(function(player)
table.insert(playersInZone, player)
local HP = game.Workspace.Crystals.HPs.HP1
if HP.Value >= 1 then
script.Parent.Parent.zone.D.Transparency = 0.9
script.Parent.Parent.zone.Part.Transparency = 0.95
local re = game.ReplicatedStorage.Zones.Crystals:WaitForChild("Crystal1re")
re:FireClient(player) --Makes the textbutton visible
elseif HP.Value == 0 then
print("hp is 0")
local re2 = game.ReplicatedStorage.Zones.Crystals:WaitForChild("Crystal1re2")
re2:FireClient(player) --Makes the textbutton not visible
end
end)
zone.playerExited:Connect(function(player)
local findPlayer = table.find(playersInZone, player)
table.remove(playersInZone, findPlayer)
local HP = game.Workspace.Crystals.HPs.HP1
if HP.Value >= 1 then
script.Parent.Parent.zone.D.Transparency = 1
script.Parent.Parent.zone.Part.Transparency = 1
local re2 = game.ReplicatedStorage.Zones.Crystals:WaitForChild("Crystal1re2")
re2:FireClient(player) --Makes the textbutton not visible
elseif HP.Value == 0 then
print("hp is 0")
local re2 = game.ReplicatedStorage.Zones.Crystals:WaitForChild("Crystal1re2")
re2:FireClient(player) --Makes the textbutton not visible
script.Parent.Parent.zone.D.Transparency = 1
script.Parent.Parent.zone.Part.Transparency = 1
end
end)
--HPTaker Script
script.Parent.MouseButton1Click:Connect(function()
local HP = game.Workspace.Crystals.HPs.HP1
HP.Value -= 1
end)