I have been stumped on writing this code for a while, i’m a newbie coder and I only know the basics. I’m trying to make an Item equip script when the item on the ground is clicked. The item in question is a pair of pliers.
So far I have tried taking the tool from the workspace and duping it into starterPack but this doesn’t seem to work
Here is my script:
local clickdetector = workspace.Pliers.ClickDetector
local pliers = workspace.Pliers
clickdetector.MouseClick:Connect(function()
local model = workspace.Pliers:Clone()
model.Parent = game.StarterGui --I dont know what to replace this with
You would need to put it in the Player.Backpack, not starter pack. Starter pack is the set of tool that the player starts with, and since you want the player to pick up the tool immediately you would add it to their Player.Backpack.
You could modify your code like so:
local clickdetector = workspace.Pliers.ClickDetector
local pliers = workspace.Pliers
clickdetector.MouseClick:Connect(function(player) -- note that click detectors tell you which player clicked on them
local model = workspace.Pliers:Clone()
model.Parent = player.Backpack -- this is where it should go
pliers:Destroy()
end)
Put it in the character instead if you want them to equip it, or their backpack if you just want them to have the item. For example:
local clickdetector = workspace.Pliers.ClickDetector
local pliers = workspace.Pliers
clickdetector.MouseClick:Connect(function(player) -- the player who clicked is the first parameter of clickdetector's MosueClick
local model = workspace.Pliers:Clone()
model.Parent = player.Character -- equip the tool directly
pliers:Destroy()
end)
Hello. Tools have to be parented to the player’s backpack so they can later equip them with Roblox’s built-in inventory system. StarterGui is used to store gui objects, while StarterPack is where you generally place tools to be in player’s inventory upon start of the game and after respawning.
Now you need to know who clicked the tool, right? That’s where playerWhoClicked argument comes. Pay attention to your script with these two modifications:
local clickdetector = workspace.Pliers.ClickDetector
local pliers = workspace.Pliers
clickdetector.MouseClick:Connect(function(Player) -- Note the "Player" thing here, Roblox will assign to this variable the player instance
local model = workspace.Pliers:Clone()
model.Parent = Player.Backpack -- that's where the tool should go to!
pliers:Destroy()
end)
Hope this helped, if you didn’t understand something just ask!
You may want to check this out, a post made by me, and use the key tutorial to pick it up?
It helps make a more efficient version of what you want to do.
Anyway, try this, insert a local script inside of starter player scripts;
I have tried every one of the current responses with the script in StarterPlayerScripts
but so far none of the responses have caused the pliers to enter my inventory and get deleted. Some of the scripts delete the item but the item never enters the inventory.
This script worked the best but every time I clicked it in my inventory it would do the thing where your character gets teleported to wherever the pliers were, even if I un-anchor the pliers