Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do you switch items when you equip another?

Asked by 3 years ago

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

Log in to vote
0
Answered by
NGC4637 602 Moderation Voter
3 years ago
Edited 3 years ago

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)
0
It sounds really promising. I'll learn the script to figure out what to do later. EmmaMallen 13 — 3y
Ad

Answer this question