Bikereh
(green_lady)
October 11, 2021, 10:43pm
1
So I have this reloading script which runs when the reload remote event fires.
--// Variables \\--
-- Variables --
local tool = script.Parent
-- Values --
local equipped = false
local reloading = tool:WaitForChild("Reloading")
local ammoFolder = tool.Ammo
local mag = ammoFolder.Mag
local maxAmmo = ammoFolder.MaxAmmo
--// Code \\--
-- Equipping --
tool.Equipped:Connect(function()
equipped = true
end)
tool.Unequipped:Connect(function()
equipped = false
reloading.Value = false
end)
-- Reloading --
tool:WaitForChild("Reload").OnServerEvent:Connect(function(plr)
if reloading.Value or not equipped or mag.Value == maxAmmo.Value then return end
reloading.Value = true
wait(0.5)
tool.BodyAttach.MagOut:Play()
wait(0.53)
tool.BodyAttach.MagIn:Play()
wait(0.6)
tool.BodyAttach.BoltBack:Play()
wait(0.27)
tool.BodyAttach.BoltForward:Play()
wait(0.77)
-- Handle Ammo --
mag.Value = maxAmmo.Value
reloading.Value = false
end)
Does anyone know how to stop reload when the player unequips the tool?
tell me if you need more info (i deleted everything about firing the gun just to make the code easier to understand)
1 Like
GrayzcaIe
(Pluto)
October 11, 2021, 10:44pm
2
Instead of using the “or” operator here, you might consider using “and”
1 Like
Kaid3n22
(Kaiden)
October 11, 2021, 10:46pm
3
add an if statement to check if the reloading value is still true.
if reloading.Value == true then
mag.Value = maxAmmo.Value
reloading.Value = false
end
1 Like
You can simply tie the script with a tool.equipped function so it only occurs when the tool is equipped.
1 Like
Bikereh
(green_lady)
October 11, 2021, 10:48pm
5
But then if the player unequips after the if statements passes if wont detect it so it won’t stop.
1 Like
Bikereh
(green_lady)
October 11, 2021, 10:48pm
6
I don’t think that makes a difference.
Kaid3n22
(Kaiden)
October 11, 2021, 10:49pm
7
Right here, you changed the reloading value to false after unequipping the tool. So, if they started to reload, it would be turned to false and not add the ammo.
Here is what I meant with tying in a equipped function.
local activateequip;
--This is called a connection.
activateequip = tool.Equipped:Connect(function()
-- your equip script here
end)
tool.Unequipped:Connect(function()
activateequip:Disconnect()
--when tool is unequipped, it disconnects the equipped function rendering it disabled
end)
Bikereh
(green_lady)
October 11, 2021, 10:54pm
9
I did this:
--// Variables \\--
-- Variables --
local tool = script.Parent
-- Values --
local equipped = false
local reloading = tool:WaitForChild("Reloading")
local ammoFolder = tool.Ammo
local mag = ammoFolder.Mag
local maxAmmo = ammoFolder.MaxAmmo
--// Code \\--
local func
-- Equipping --
tool.Equipped:Connect(function()
equipped = true
func = tool:WaitForChild("Reload").OnServerEvent:Connect(function(plr)
if reloading.Value or not equipped or mag.Value == maxAmmo.Value then return end
reloading.Value = true
wait(0.5)
tool.BodyAttach.MagOut:Play()
wait(0.53)
tool.BodyAttach.MagIn:Play()
wait(0.6)
tool.BodyAttach.BoltBack:Play()
wait(0.27)
tool.BodyAttach.BoltForward:Play()
wait(0.77)
-- Handle Ammo --
mag.Value = maxAmmo.Value
reloading.Value = false
end)
end)
tool.Unequipped:Connect(function()
equipped = false
reloading.Value = false
func:Disconnect()
end)
-- Reloading --
tool:WaitForChild("Reload").OnServerEvent:Connect(function(plr)
if reloading.Value or not equipped or mag.Value == maxAmmo.Value then return end
reloading.Value = true
wait(0.5)
tool.BodyAttach.MagOut:Play()
wait(0.53)
tool.BodyAttach.MagIn:Play()
wait(0.6)
tool.BodyAttach.BoltBack:Play()
wait(0.27)
tool.BodyAttach.BoltForward:Play()
wait(0.77)
-- Handle Ammo --
mag.Value = maxAmmo.Value
reloading.Value = false
end)
It didn’t work. (i think that’s what you meant)
Remove “equipped” and “unequipped” variables, also you need two connections as you have another function below for reloading
Also please make the connection the tool equipped function
Bikereh
(green_lady)
October 11, 2021, 10:58pm
12
Sorry, but can you show me how to do that. lol
Bikereh
(green_lady)
October 11, 2021, 11:00pm
13
Oh sorry i tried it again but with this:
--// Variables \\--
-- Variables --
local tool = script.Parent
-- Values --
local equipped = false
local reloading = tool:WaitForChild("Reloading")
local ammoFolder = tool.Ammo
local mag = ammoFolder.Mag
local maxAmmo = ammoFolder.MaxAmmo
--// Code \\--
local func
-- Equipping --
tool.Equipped:Connect(function()
equipped = true
func = tool:WaitForChild("Reload").OnServerEvent:Connect(function(plr)
if reloading.Value or not equipped or mag.Value == maxAmmo.Value then return end
reloading.Value = true
wait(0.5)
tool.BodyAttach.MagOut:Play()
wait(0.53)
tool.BodyAttach.MagIn:Play()
wait(0.6)
tool.BodyAttach.BoltBack:Play()
wait(0.27)
tool.BodyAttach.BoltForward:Play()
wait(0.77)
-- Handle Ammo --
mag.Value = maxAmmo.Value
reloading.Value = false
end)
end)
tool.Unequipped:Connect(function()
equipped = false
reloading.Value = false
func:Disconnect()
end)
But I got the same results.
--// Variables \\--
-- Variables --
local tool = script.Parent
-- Values --
local equipped = false
local reloading = tool:WaitForChild("Reloading")
local ammoFolder = tool.Ammo
local mag = ammoFolder.Mag
local maxAmmo = ammoFolder.MaxAmmo
--// Code \\--
local func;
-- Equipping --
func = tool.Equipped:Connect(function()
equipped = true
tool:WaitForChild("Reload").OnServerEvent:Connect(function(plr)
if reloading.Value or not equipped or mag.Value == maxAmmo.Value then return end
reloading.Value = true
wait(0.5)
tool.BodyAttach.MagOut:Play()
wait(0.53)
tool.BodyAttach.MagIn:Play()
wait(0.6)
tool.BodyAttach.BoltBack:Play()
wait(0.27)
tool.BodyAttach.BoltForward:Play()
wait(0.77)
-- Handle Ammo --
mag.Value = maxAmmo.Value
reloading.Value = false
end)
end)
tool.Unequipped:Connect(function()
equipped = false
reloading.Value = false
func:Disconnect()
end)
Try that, tell me if any errors occur because it’s quite hard to tell on the DevForum
You need to add ; after func other wise it is just a variable, not a connection
Bikereh
(green_lady)
October 11, 2021, 11:08pm
17
no errors it’s the same as before (i also added the ; after func)
Bikereh
(green_lady)
October 11, 2021, 11:09pm
18
I think its because the reloading has waits inside of it. I put them there because my sounds are all seperate
Can I ask why you have two reloading functions?
Bikereh
(green_lady)
October 11, 2021, 11:10pm
20
i took the other one away but the same thing happens