Tool has 5 uses before it can’t work. This is just a code snippet.
I am testing to make sure when Mouse.Button1Up or ToolUnequipped or Mouse.Move it makes lua i
go to 0. But, print(i…" after") prints the i BEFORE i = i + 1. So that means i goes up each time no matter what. I am lost and do not know what to do, can someone help guide me?

local function countDown()
print('countdown called')
local sandBagsCopy = BuiltSandBags:Clone()
while mouseDown do
wait()
if mouseDown and i <= 5 then
wait(1)
print(i..' before')
i = i + 1
print(i..' after')
end
if i >= 5 then
remoteEvent:FireServer(posX, posY, posZ, sandBagsCopy)
uses = uses + 1
i = 0
--sandBagsCopy.Parent = game.Workspace
--sandBagsCopy.Position = Vector3.new(posX, posY, posZ)
wait()
end
if uses >= 5 then
script.Parent:Destroy()
end
end
end
So is the tool not destroying itself after 5 uses or is it destroying itself prematurely? On a side note, it might also help if we could see where the script is placed.
No no- this is NOT a directory issue, but an issue within the script itself. I think it might have something to do with ‘while mouseDown do’ Maybe I should try using a for loop? Or what do you think?
In that case can I know more about what the script does, as in is it destroying itself prematurely or failing to do so even after 5 tries? It may also help if I can see the full log and more parts of the script too, because the above snippet isn’t showing me when you’re calling this function, only its functionality.
Full Script
Let me be more clear: so every 5 seconds IF Button1Down it creates something, then after 5 creations the tool deletes itself.
I’m trying to make it so, if it’s in the 3rd out of 5 seconds, and you unequip the tool, the seconds counted (i) gets reset to 0. but it keeps adding 1.

local Tool = script.Parent
local mouse = game.Players.LocalPlayer:GetMouse()
local equipped = false
local mtarget = game.ReplicatedStorage.PlacementSandbags
local BuiltSandBags = game.ReplicatedStorage.BuiltSandBags
local mouseDown = false
local i = 0
local uses = 0
local remoteEvent = game.ReplicatedStorage.BuildSandbagsEvent
local isDown = false
mouseDown = false
local function countDown()
print('countdown called')
local sandBagsCopy = BuiltSandBags:Clone()
while mouseDown do
wait()
if mouseDown and i <= 5 then
wait(1)
print(i..' before')
i = i + 1
print(i..' after')
end
if i >= 5 then
remoteEvent:FireServer(posX, posY, posZ, sandBagsCopy)
uses = uses + 1
i = 0
wait()
end
if uses >= 5 then
script.Parent:Destroy()
end
end
end
mouse.Button1Down:Connect(function()
if not isDown then
isDown = true
mouseDown = true
countDown()
end
end)
mouse.Button1Up:Connect(function()
if isDown then
isDown = false
mouseDown = false
i = 0
end
end)
local function toolEquipped()
equipped = true
sandBagsPlaceCopy = mtarget:Clone()
sandBagsPlaceCopy.Parent = workspace
end
local function toolUnEquipped()
equipped = false
mouseDown = false
i = 0
sandBagsPlaceCopy:Destroy()
end
local function mouseMove()
if equipped and mouse.Target.Name == "Baseplate" then
mouseDown = false
i = 0
posX, posY, posZ = mouse.Hit.X, mouse.Hit.Y, mouse.Hit.Z
posY = posY + 1
sandBagsPlaceCopy.Position = Vector3.new(posX, posY, posZ)
end
end
Tool.Equipped:Connect(toolEquipped)
Tool.Unequipped:Connect(toolUnEquipped)
mouse.Move:Connect(mouseMove)
So am I right to say that when i is supposed to reset to 0, it keeps on adding 1 to itself instead?
If that’s the case, you might want to shift the order of these few lines in countDown(). When you reset i to 0, this part may still be yielding, so i eventually has 1 added to it. If you put it ahead, there’s a much, much higher chance that i will be set to 0 after the addition has been made.
if mouseDown and i <= 5 then
print(i..' before')
i = i + 1
wait(1)
print(i..' after')
end
Thank you!
So my question is, when my I =i + 1 was behind wait, why did it keep adding one to itself, where as when I move that line after wait, it works?
If you put wait(1) on top, this is the sequence of events that take place when you unequip the tool.
(i is still at 0)
- The function connected to Button1Up runs, setting i to 0
- The while loop is still yielding because of wait(1)
- The yielding finishes and 1 is added to i, therefore i is 1
If you were to put wait(1) below, this is what would happen if you unequipped the tool.
- There is no yield, so 1 would already have been added to i.
- The function connected to Button1Up runs, setting i to 0
- The while loop has already ended because by now, mouseDown is false. Therefore, i no longer has 1 to added to it
The sequence that you carry out your tasks can have a very different impact on the overall result when it comes to scripting. Hope this helps.
1 Like