I need help cloning a tool into the players’ backpack.
This is my script:
local ServerStorage = game:GetService("ServerStorage")
local player = game.Players.LocalPlayer
local function Grab()
-- what to go here?
end
script.Parent.ProximityPrompt.Triggered:Connect(Grab)
Make sure it’s a server script if it isn’t, and change your code to this:
--//Services
local ServerStorage = game:GetService("ServerStorage")
--//Variables
local Tool = ServerStorage.MyCoolTool --//Reference your tool
local Part = script.Parent
local ProximityPrompt = Part.ProximityPrompt
--//Functions
ProximityPrompt.Triggered:Connect(function(player)
local newTool = Tool:Clone()
newTool.Parent = player.Backpack
end)
If you want it to only be grabbed once, here’s the code for that:
--//Services
local ServerStorage = game:GetService("ServerStorage")
--//Variables
local Tool = ServerStorage.MyCoolTool --//Reference your tool
local Part = script.Parent
local ProximityPrompt = Part.ProximityPrompt
--//Functions
ProximityPrompt.Triggered:Connect(function(player)
if player:FindFirstChild(Tool.Name) then
return
end
local newTool = Tool:Clone()
newTool.Parent = player.Backpack
end)
local ServerStorage = game:GetService("ServerStorage")
script.Parent.ProximityPrompt.Triggered:Connect(function(Player)
if Player.Backpack:FindFirstChild("ToolName") or Player.Character:FindFirstChild("ToolName") then
print("Player has tool already")
else
local Tool = ServerStorage:WaitForChild("ToolName"):Clone()
Tool.Parent = Player.Backpack
end
end)
Try do something like this, it worked fine for me.
local Tool = ST.Tool --tool
local PP = script.Parent --proximityprompt
PP.Triggered:Connect(
function(player)
if player.Backpack:FindFirstChild(Tool.Name) then
return
end
local newTool = Tool:Clone()
newTool.Parent = player.Backpack
end
)