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)

6 comments

0

The problem lies from line 60 to line 69 if you were wondering.

0

try removing the if statement because you're setting it to false before the if statement and after you set the variable to damage as true

0

The Button1Down / Button1Up event listeners are nested in your Equipped event listeners, meaning if someone spam equips/unequipped when they equip a new listener is established. So your codes are executing 5092948482 times when the button is pressed. Don't use Button1Down at all and instead use tool.Activated and tool.Deactivated. And not nested.

0

Ok

0

And that Button1Down makes sense because I have made a gun before and you could spam click 500 rays per second.

0

It still spams visibility.