How can I make this AncestryChanged actually run?

i know i know, title is very misleading

but basically, i’m making reloading right

reloading only runs when a boolvalue called reloading is set to true, via a while loop

to ensure that the reloading doesn’t continue after the gun gets unequipped, i’ve decided to put an AncestryChanged event to use

well obviously silly me didn’t think this through and now i’m stuck at a probably very-simple-to-fix problem

i’ll comment out the unnecessary stuff

--local ReloadEvent = game:GetService("ReplicatedStorage"):WaitForChild("GunEvents").ReloadEvent
--local gunInfo = require(game:GetService("ServerScriptService"):WaitForChild("Weapons"):WaitForChild("Gun").GunStats)



--ReloadEvent.OnServerEvent:Connect(function(player)
	local tool = player.Character:FindFirstChildOfClass("Tool")	
	local reloading = tool.Reloading.Value

	--if tool.Bullets.Value > gunInfo[tool.Name]["MAX_BULLETS"] then player:Kick("Gluttony is a sin.") return end
	--if reloading then player:Kick("Very funny.") return end

	tool.AncestryChanged:Connect(function()
		reloading = false
	end)

	-- // NORMAL RELOAD
	--if tool.Bullets.Value > 0 then
		reloading = true

		--local NORMAL_MAGAZINE_PULL_TIME = gunInfo[tool.Name]["NORMAL_MAGAZINE_PULL_TIME"]
		--local NORMAL_MAGAZINE_INSERT_TIME = gunInfo[tool.Name]["NORMAL_MAGAZINE_INSERT_TIME"]
		--local NORMAL_FINISH_RELOAD_TIME = gunInfo[tool.Name]["NORMAL_FINISH_RELOAD_TIME"]

		--local PullMagazineSound = tool.Magazine.PullMagSound
		--local InsertMagazineSound = tool.Magazine.InsertMagSound


		while reloading do
			task.wait(NORMAL_MAGAZINE_PULL_TIME)
			PullMagazineSound:Play()
			tool.Bullets.Value = 0

			task.wait(NORMAL_MAGAZINE_INSERT_TIME)
			InsertMagazineSound:Play()
			tool.Bullets.Value = gunInfo[tool.Name]["MAX_BULLETS"]

			task.wait(NORMAL_FINISH_RELOAD_TIME)
			reloading = false
		end
		-------------------------------------------------------------------------------

		-- // EMPTY RELOAD
	--elseif tool.Bullets.Value == 0 then
		reloading = true

		--local EMPTY_MAGAZINE_PULL_TIME = gunInfo[tool.Name]["EMPTY_MAGAZINE_PULL_TIME"]
		--local EMPTY_MAGAZINE_INSERT_TIME = gunInfo[tool.Name]["EMPTY_MAGAZINE_INSERT_TIME"]
		--local EMPTY_BOLT_PULL_TIME = gunInfo[tool.Name]["EMPTY_BOLT_PULL_TIME"]
		--local EMPTY_FINISH_RELOAD_TIME = gunInfo[tool.Name]["EMPTY_FINISH_RELOAD_TIME"]

		--local PullMagazineSound = tool.Magazine.PullMagSound
		--local InsertMagazineSound = tool.Magazine.InsertMagSound
		--local BoltPullSound = tool.Bolt.PullBoltSound


		while reloading do
			task.wait(EMPTY_MAGAZINE_PULL_TIME)
			PullMagazineSound:Play()
			tool.Bullets.Value = 0

			task.wait(EMPTY_MAGAZINE_INSERT_TIME)
			InsertMagazineSound:Play()
			tool.Bullets.Value = gunInfo[tool.Name]["MAX_BULLETS"]

			task.wait(EMPTY_BOLT_PULL_TIME)
			BoltPullSound:Play()

			task.wait(EMPTY_FINISH_RELOAD_TIME)
			reloading = false
		end
	end
end)

i’m trying to avoid having a AncestryChanged event along with a function between every single task.wait(), is there a way i could have that event trigger whenever?

Yes. Instead of using AncestryChanged, you can use the Tool.Unequipped event to do that instead. For instance:

tool.Unequipped:Connect(function()
  reloading = false
end)

doesn’t do the trick sadly charrrrrrrrrrrrrr

I see. I want to make sure that I give you info that will actually help, so I just want to make sure real quick.

I think what you’re asking is if there’s a way to check if the gun is still equipped during the reload sequence so that it won’t continue if it isn’t. Is that right?

If so, then in your while reloading do loop, after each task.wait, I’d add a if not reloading then break end statement to break the loop if the gun is unequipped.

1 Like

ah yeah that did the trick, thanks!!!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.