Hello DevForums, I have been working on a tool script, and I wanted the tool to swing left and right. I used math.Random, but it only swings one side. This is also not the fault of my animations id being the same, they are both different ids.
Heres the Script: btw this is a local script
local tool = script.Parent
local localplayer = game:GetService(“Players”).LocalPlayer
local humanoid = localplayer.Character.Humanoid
local db = false
tool.Activated:Connect(function()
local choose = math.random(1,2)
if choose == 1 then
if not db then
db = true
tool.Blade.Trail.Enabled = true
local swinganim = humanoid:LoadAnimation(script.MacheteSwing)
swinganim:Play()
script.Swing:Play()
tool.Attack.Disabled = false
wait(0.4)
db = false
wait(0.2)
tool.Attack.Disabled = true
tool.Blade.Trail.Enabled = false
elseif choose == 2 then
if not db then
db = true
tool.Blade.Trail.Enabled = true
local swinganim2 = humanoid:LoadAnimation(script.MacheteSwing2)
swinganim2:Play()
script.Swing:Play()
tool.Attack.Disabled = false
wait(0.4)
db = false
wait(0.2)
tool.Attack.Disabled = true
tool.Blade.Trail.Enabled = false
end
end
end
end)
But anyways create to local functions.
This might not work since I can not test but at least try it.
-- add this above the activation event
local function Swing1()
if not db then
db = true
tool.Blade.Trail.Enabled = true
local swinganim = humanoid:LoadAnimation(script.MacheteSwing)
swinganim:Play()
script.Swing:Play()
tool.Attack.Disabled = false
wait(0.4)
db = false
wait(0.2)
tool.Attack.Disabled = true
tool.Blade.Trail.Enabled = false
end
end
-- now add the second one
local function Swing2()
if not db then
db = true
tool.Blade.Trail.Enabled = true
local swinganim2 = humanoid:LoadAnimation(script.MacheteSwing2)
swinganim2:Play()
script.Swing:Play()
tool.Attack.Disabled = false
wait(0.4)
db = false
wait(0.2)
tool.Attack.Disabled = true
tool.Blade.Trail.Enabled = false
end
end
-- this is inside the activated event
local choose = math.random(1,2)
if choose == 1 then
Swing()
elseif choose == 2 then
Swing2()
end
end)
Here’s your code with just the if statements and with proper indentation.
tool.Activated:Connect(function()
local choose = math.random(1,2)
if choose == 1 then
if not db then
db = true
-- ...
db = false
elseif choose == 2 then
if not db then
db = true
-- ...
db = false
end
end
end
end)
If choose is 2, then nothing will happen at all. Your sword will not swing at all.
The solution is to make it look like this:
tool.Activated:Connect(function()
local choose = math.random(1,2)
if choose == 1 then
if not db then
db = true
-- ...
db = false
end
elseif choose == 2 then
if not db then
db = true
-- ...
db = false
end
end
end)
You can turn db into a sentinel so you only have to check for it in one place.
tool.Activated:Connect(function()
if not db then return end -- if sword is swinging, don't do anything
local choose = math.random(1,2)
if choose == 1 then
db = true
-- ...
db = false
elseif choose == 2 then
db = true
-- ...
db = false
end
end)
Or possibly even:
tool.Activated:Connect(function()
if not db then return end -- if sword is swinging, don't do anything
db = true -- mark that the sword is swinging
local choose = math.random(1,2)
if choose == 1 then
-- ...
elseif choose == 2 then
-- ...
end
db = false -- allow the sword to swing again
end)