I am creating this mining ore for no reason what so ever and I set up a health bar and I want it to stay while the player is mining but I want it to go away while they're not. The problem is, it glitches and stuff like that. Whenever you spam click the ore it will pop up then go back down really quickly.
Local Script:
local cursors = { equip = "rbxassetid://117431027", unequip = "" } local tool = script.Parent local player = game.Players.LocalPlayer repeat wait() print("Waiting for player...") until player local character = player.Character repeat wait() print("Waiting for character...") until character local box = script.Parent:WaitForChild("Selector") local mainUI = player.PlayerGui:FindFirstChild("MainUI") local damageEvent = game:GetService("ReplicatedStorage").Remotes.DamageOre local deleteEvent = game:GetService("ReplicatedStorage").Remotes.DeleteOre local config = require(script.Parent:WaitForChild"Config") local damage = false tool.Equipped:Connect(function(mouse) mouse.Icon = cursors.equip mouse.Move:Connect(function() if mouse.Target then if mouse.Target.Name == "Ore" then local humanoid = character:FindFirstChild("HumanoidRootPart") if(mouse.Target.Position - humanoid.Position).magnitude < 30 then if mouse.Target:FindFirstChild("Selector") then return end box.Visible = true box.Adornee = mouse.Target else box.Visible = false box.Adornee = nil end else box.Visible = false box.Adornee = nil end end end) mouse.Button1Down:Connect(function() if mouse.Target then if mouse.Target.Name == "Ore" then local target = mouse.Target local oreName = target:WaitForChild("Name") local oreHealth = target:WaitForChild("Health") local oreMaxHealth = target:WaitForChild("MaxHealth") local oreHealthUI = mainUI.OreHealth local oreCurrentHealth = oreHealth.Value/oreMaxHealth.Value oreHealthUI.Visible = true oreHealthUI.HealthBar.Percent.Text = oreHealth.Value.."/"..oreMaxHealth.Value.."%" oreHealthUI.HealthBar.Fill:TweenSize(UDim2.new(oreCurrentHealth, 0,1, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Linear, 0.2) if oreHealth.Value == 0 then deleteEvent:FireServer(target) end damageEvent:FireServer(target, config.damage) damage = true wait(3) damage = false if damage == false then oreHealthUI.Visible = false end end end end) end)