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

How do you switch items when you equip another?

I'm making an adventure game and keys are needed to escape.

However, the your old tool needs to be swapped with the item that you equipped before.

This is to prevent players technically getting all the items in a really short matter of time, and making it fair for others in the match.

However, i'm not sure how to make that since I don't know much about tools.

Any help?

1 answer

I will tell you what you need the script to do.

you have to make it so that when something is added to the LocalPlayer's backpack, it will call a function. (which means you have to use a local script)

then inside the function, you have to get the children of the backpack and remove all items, then re add the item that the player got into the backpack.

Here's a local script that may help (make sure to put in StarterPlayerScripts):

game.Players.LocalPlayer:WaitForChild("Backpack") --waits for the backpack to load (just in case)

--Variables:
local plr = game.Players.LocalPlayer 
local bp = plr.Backpack

bp.ChildAdded:Connect(function(child) --calls a function when a child is added to the backpack
	if v:IsA("Tool") then --checks if the item added is a Tool
		local Key = child:Clone() --Clones the added child because we will destroy everything in the backpack
		for _,v in pairs(bp:GetChildren()) do --gets every children of the backpack
			v:Destroy() --destroys them all
		end
		Key.Parent = bp --readds the child to the backpack
	end
end)

1 comment

0

It sounds really promising. I'll learn the script to figure out what to do later.

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