How do I make spawner detect if player picks up weapon?

I want to have a weapon spawner only start the spawner timer after the player equips the tool. my reasoning is because the weapons will continue to spawn on each other. I tried to check if the tool clones parent was not in the workspace but it didn’t work.

part = script.Parent
Rs = game:GetService("ReplicatedStorage")
Weapons = Rs.Common

function SpawnRandomWeapon()
	local WeaponsTable = Weapons:GetChildren()
	local RandomWeapon = WeaponsTable[math.random(1, #WeaponsTable)]
	local WeaponClone = RandomWeapon:Clone()
	WeaponClone.Parent = workspace["Common Spawner"]
	WeaponClone.Handle.CFrame = part.CFrame
	
	if WeaponClone.Parent ~= workspace then
		print("not in workspace")
	end
end

SpawnRandomWeapon()



while wait(math.random(1, 6)) do
	SpawnRandomWeapon()
end
1 Like

Could have like a variable for the last weapon that was spawned/cloned. Then could change the while loop to something like

while task.wait() do
     if LastClonedWeapon.Parent ~= workspace["Common Spawner"} then
         task.wait(math.random(1,6))
         SpawnRandomWeapon()

Something like that in theory should work

2 Likes

Would this work?

part = script.Parent
Rs = game:GetService("ReplicatedStorage")
Weapons = Rs.Common

function SpawnRandomWeapon()
	local WeaponsTable = Weapons:GetChildren()
	local RandomWeapon = WeaponsTable[math.random(1, #WeaponsTable)]
	local WeaponClone = RandomWeapon:Clone()
	WeaponClone.Handle.CFrame = part.CFrame
	WeaponClone.Parent = workspace["Common Spawner"]
	repeat task.wait() until WeaponClone.Parent ~= workspace["Common Spawner"]
	print("not in workspace")
end

SpawnRandomWeapon()



while task.wait(math.random(1, 6)) do
   SpawnRandomWeapon() 
end

I’m assuming that you only want a weapon to spawn when a weapon isn’t already under the spawner. Try it out and let me know if this is what you want.

2 Likes

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