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

Delete Previous gear after its been bought?

title might be confusing but, i'm making a button that gives you a weapon on a certain level, but to avoid taking up room in the backpack, i need it to delete the previous gear, or all gears before placing the new gear down, the thing i really need help with is how to get all the tools in the players backpack, i don't really know how to define that.

Here is my current Script

Points = 10 
Stat = "Points" 



Give = script.Parent
Backup = Give:Clone()

function onTouched(hit)
	if hit.Parent.Humanoid~= nil then
		local Plr = game.Players:findFirstChild(hit.Parent.Name)
		if Plr ~= nil then
			local StatLocal = Plr.leaderstats:findFirstChild(Stat)
			if StatLocal ~= nil then
				if StatLocal.Value >= Points then
					local Check = Plr.StarterGear:findFirstChild(script.Parent.Name)
					if Check == nil then
						StatLocal.Value = StatLocal.Value - Points
						
						
						local W1 = Backup:Clone()
						local W2 = Backup:Clone()
						W1.Parent = Plr.Backpack
						W2.Parent = Plr.StarterGear
					end
				end
			end
		end
	end
end
script.Parent.Parent.Touched:connect(onTouched)

this script does the job of putting the weapon in your backpack AND Starterpack, so if you die, you'll get the weapon back

1 answer

✓ Accepted answer

You could write a simple function that iterates through all the children of an object and destroys them. Fire this function before you add the new "gear". That way there is a clean slate to add gear to.

Using :GetChildren() returns a table of all children of the object it is called on.

For example:

local function DestroyChildren(object)
	for i,v in pairs(object:GetChildren()) do
		if v:IsA("Tool") then
			v:Destroy()
		end
	end
end

Change the argument of the function to StarterPack or BackPack and call it prior to inserting new gear. I hope this helps! Let me know if you have any questions!

10 comments

0

where do i put the Local Function bit? and where do i put the The `DestroyChildren()'

0

For some reason this wont work, the output says that Line 2 of your example is wrong, wherever i put the function and when it fires, is it wrong? Is it because the script is inside the gear that given when the button is pressed

0

The script should be inside ServerScriptService. The DestroyChildren() function should be prior to your main script and then called in your main script right before you add new gear.

0

Why do i still not get this?

0

is this got to do with remoteEvents?

0

because i dont do well with events

0

could you maybe re-write this, because im confused, putting the script in serverscriptservice just makes it 10x worse as the regular script is trying to get the parent (which is the gear), and your saying to put the function into a new script(?, or not), or to put the main script inside serverscriptservice, i feel like reposting the question

0

Send me a message on Roblox with your Discord and I'll walk you through step by step. Afterwords I'll update the answer with what we accomplished.

0

ok

1

you are a very good teacher

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