Just as an example, in piggy, when you click a new item, the old item in your backpack disappears. The same thing is what I want to replicate in my game.
So i have an item (a key) that contains a click detector and the following Script:
local tool = game.ServerStorage.tools.redkeytool local klone = tool:clone() script.Parent.ClickDetector.MouseClick:Connect(function(plr) if klone.Parent ~= plr.Backpack then klone.Parent = plr.Backpack plr.Backpack.keyid.Value = "red" else end if plr.Backpack.keyid.Value == "blue" then plr.Backpack.bluekeytool:Destroy() end if plr.Backpack.keyid.Value == "green" then plr.Backpack.greenkeytool:Destroy() end if plr.Backpack.keyid.Value == "silver" then plr.Backpack.silverkeytool:Destroy() end if plr.Backpack.keyid.Value == "purple" then plr.Backpack.purplekeytool:Destroy() end if plr.Backpack.keyid.Value == "wood" then plr.Backpack.woodtool:Destroy() end if plr.Backpack.keyid.Value == "wirecutters" then plr.Backpack.wirecutterstool:Destroy() end if plr.Backpack.keyid.Value == "orange" then plr.Backpack.orangekeytool:Destroy() end if plr.Backpack.keyid.Value == "gold" then plr.Backpack.golddkeytool:Destroy() end end)
(keyid is a StringValue in the players backpack so the game knows what the player is holding)
It returns with the error saying keyid is not a valid member of Backpack.
Although I made a localscript that clones "keyid" into the players backpack:
print("Hello world!") local keyid = game.Workspace.keyid local keyid2 = keyid:Clone() keyid2.Parent = game.Players.LocalPlayer:WaitForChild("Backpack")
UPDATE:
Okay so I got it to work using FindFirstChild()
, but now if i were to take a key (key 1), then replace it with another key (key 2), and then go back to key 1 to replace it with key 2, it says:
the current parent of redkey is locked, currect parent: NULL, new parent: backpack
Not sure what this means
Hey, iiDoge_Legend
To create this system, it is actually quite simple. Let's start off by defining some variables.
local serverStorage = game:GetService("ServerStorage") local tool = serverStorage:FindFirstChild("tools").redkeytool
Once we have our variables we can begin to create our function
local serverStorage = game:GetService("ServerStorage") local tool = serverStorage:FindFirstChild("tools").redkeytool script.Parent.ClickDetector.MouseClick:Connect(function(plr) if plr.Character:FindFirstChild("Humanoid").Health > 0 then if not plr.Backpack:FindFirstChild("redkeytool") and not plr.Character:FindFirstChild("redkeytool") then for i, v in pairs(plr.Backpack:GetChildren())do if v.Name ~= "keyid" then v:Destroy() end end plr.Backpack.keyid.Value = "red" local clonedTool = tool:Clone() clonedTool.Parent = plr.Backpack end end end)
The first IF
statement checks whether the player is dead or not. Because I am pretty sure ROBLOX still allows players to use click detectors when dead. After that, we check to make sure that the redkeytool is not already in the player's backpack. On that same line, we also check to make sure it is not inside of the character. Because when you equip a tool it actually takes it out of the player's backpack and puts it inside of their character. If so then we are good to go. We then use a for loop to iterate through the player's backpack and delete everything that is not the keyid. By the way, you can also use
if v:IsA("Tool") then end
to check if it is a tool or not. Once we delete all other tools from the players backpack we then change the value of keyid and clone the tool into the player's backpack.
I hope this helped you out, leave a comment below if you have any questions and if this did solve your issue make sure to upvote and accept this as an answer so other people on the forum know that it has been answered.