Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How would I make a delay without using wait() or a debounce?

I'm making a tool that can chop trees and damage players. When I tested it out, it worked. But the problem is, theres no delay. It seemed like it can easily be fixed with a wait() or a debounce. But that wasn't the case. Even if there was a delay, it wouldn't prevent the spamming. I tried putting the wait() near the mouse.Button1Down event but that didn't work either. Heres my localscript:

local tool = script.Parent
local idle = script.Parent:WaitForChild("AxeIdle")
local swing = script.Parent:WaitForChild("AxeSwing")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local target = mouse.Target
local Rep = game:GetService("ReplicatedStorage")

local MaxChoppingDistance = 10
local Damage = 10
local HitDamage = 0.016

tool.Equipped:Connect(function()	

local anim1 = player.Character:FindFirstChild("Humanoid"):LoadAnimation(swing)
local anim2 = player.Character:FindFirstChild("Humanoid"):LoadAnimation(idle)
	for i, Objects in pairs(tool.Objects:GetChildren()) do
		if Objects:IsA("NumberValue") then
			for i, Children in pairs(Objects:GetChildren()) do
				if Children:IsA("NumberValue") then
					mouse.Button1Down:Connect(function()
						if mouse.Target.Parent == workspace:FindFirstChild(Objects.Name) then
							if (player.Character.HumanoidRootPart.Position - workspace[Objects.Name][Children.Name].Position).Magnitude < MaxChoppingDistance then
								
								local ObjectChopped = Objects.Name
								local event = Rep.Events.ChopObject
								local objectHealth = player.PlayerGui.ObjectHealth.MainFrame
								
								workspace[ObjectChopped].Health.Changed:Connect(function()
									objectHealth.Visible = true
									objectHealth.ObjectName.Text = ObjectChopped
									objectHealth.HealthLabel.Text = workspace[ObjectChopped].Health.Value .. "/" .. workspace[ObjectChopped].MaxHealth.Value
										
									if workspace[ObjectChopped].Health.Value <= 0 then
										objectHealth.Visible = false
										objectHealth.ObjectName.Text = "..."
										objectHealth.HealthLabel.Text = "0/0"
									end
								end)
								
								anim1:Play()
								anim2:Stop()
								event:FireServer(ObjectChopped, Damage)
								wait(0.5)
								anim1:Stop()
								anim2:Play()
							else
								
							end
						else
							if mouse.Target.Parent:FindFirstChildWhichIsA("Humanoid") then
								if (player.Character.HumanoidRootPart.Position - workspace[mouse.Target.Parent.Name]:FindFirstChild("HumanoidRootPart").Position).Magnitude < MaxChoppingDistance then
									local PlayerHitted = workspace[mouse.Target.Parent.Name]
									local event = Rep.Events.DamagePlayer
									local objectHealth = player.PlayerGui.ObjectHealth.MainFrame
									
									PlayerHitted:FindFirstChild("Humanoid").Changed:Connect(function()
										objectHealth.Visible = true
										objectHealth.ObjectName.Text = PlayerHitted.Name
										objectHealth.HealthLabel.Text = PlayerHitted:FindFirstChild("Humanoid").Health .. "/" .. PlayerHitted:FindFirstChild("Humanoid").MaxHealth
										
										if PlayerHitted:FindFirstChild("Humanoid").Health <= 0 then
											objectHealth.Visible = false
											objectHealth.ObjectName.Text = "..."
											objectHealth.HealthLabel.Text = "0/0"
										end
									end)
									
									anim1:Play()
									anim2:Stop()
									event:FireServer(PlayerHitted, HitDamage)
									wait(0.5)
									anim1:Stop()
									anim2:Play()
								end
							end
						end
					end)
				end
			end
		end
	end
end)

4 comments

0

try tick()

0

ive never used it but it might just work

0

What do you mean tick. Tick returns a local time since 1970 Epock

0

Use wait() and debounce at the same time.

1 answer

Try this

local idle = script.Parent:WaitForChild("AxeIdle")
local swing = script.Parent:WaitForChild("AxeSwing")
local player = game.Players.LocalPlayer
local debounce = true
local mouse = player:GetMouse()
local target = mouse.Target
local Rep = game:GetService("ReplicatedStorage")

local MaxChoppingDistance = 10
local Damage = 10
local HitDamage = 0.016

tool.Equipped:Connect(function()    
if debounce then
delay (1)
debounce = false
local anim1 = player.Character:FindFirstChild("Humanoid"):LoadAnimation(swing)
local anim2 = player.Character:FindFirstChild("Humanoid"):LoadAnimation(idle)
    for i, Objects in pairs(tool.Objects:GetChildren()) do
        if Objects:IsA("NumberValue") then
            for i, Children in pairs(Objects:GetChildren()) do
                if Children:IsA("NumberValue") then
                    mouse.Button1Down:Connect(function()
                        if mouse.Target.Parent == workspace:FindFirstChild(Objects.Name) then
                            if (player.Character.HumanoidRootPart.Position - workspace[Objects.Name][Children.Name].Position).Magnitude < MaxChoppingDistance then

                                local ObjectChopped = Objects.Name
                                local event = Rep.Events.ChopObject
                                local objectHealth = player.PlayerGui.ObjectHealth.MainFrame

                                workspace[ObjectChopped].Health.Changed:Connect(function()
                                    objectHealth.Visible = true
                                    objectHealth.ObjectName.Text = ObjectChopped
                                    objectHealth.HealthLabel.Text = workspace[ObjectChopped].Health.Value .. "/" .. workspace[ObjectChopped].MaxHealth.Value

                                    if workspace[ObjectChopped].Health.Value <= 0 then
                                        objectHealth.Visible = false
                                        objectHealth.ObjectName.Text = "..."
                                        objectHealth.HealthLabel.Text = "0/0"
                                    end
                                end)

                                anim1:Play()
                                anim2:Stop()
                                event:FireServer(ObjectChopped, Damage)
                                wait(0.5)
                                anim1:Stop()
                                anim2:Play()
                            else

                            end
                        else
                            if mouse.Target.Parent:FindFirstChildWhichIsA("Humanoid") then
                                if (player.Character.HumanoidRootPart.Position - workspace[mouse.Target.Parent.Name]:FindFirstChild("HumanoidRootPart").Position).Magnitude < MaxChoppingDistance then
                                    local PlayerHitted = workspace[mouse.Target.Parent.Name]
                                    local event = Rep.Events.DamagePlayer
                                    local objectHealth = player.PlayerGui.ObjectHealth.MainFrame

                                    PlayerHitted:FindFirstChild("Humanoid").Changed:Connect(function()
                                        objectHealth.Visible = true
                                        objectHealth.ObjectName.Text = PlayerHitted.Name
                                        objectHealth.HealthLabel.Text = PlayerHitted:FindFirstChild("Humanoid").Health .. "/" .. PlayerHitted:FindFirstChild("Humanoid").MaxHealth

                                        if PlayerHitted:FindFirstChild("Humanoid").Health <= 0 then
                                            objectHealth.Visible = false
                                            objectHealth.ObjectName.Text = "..."
                                            objectHealth.HealthLabel.Text = "0/0"
                                        end
                                    end)
debounce = true

                                    anim1:Play()
                                    anim2:Stop()
                                    event:FireServer(PlayerHitted, HitDamage)
                                    wait(0.5)
                                    anim1:Stop()
                                    anim2:Play()
                                end
                            end
                        end
                    end)
                end
            end
        end
    end
end)
end

5 comments

0

he said WITHOUT wait

0

??

0

i already tried debounces and it didn't work, i even stated it in my question, but thanks anyways.

0

Thats what I tried. It doesn't prevent spamming.

This discussion is preserved as it was. New answers, comments, and votes are closed.