i am struggling to figure out the code to get this “Gun” into the players toolbar.
i want it where when they touch the part the tool comes out of server storage and into the players toolbar. and when they leave it goes back into sever storage.
Read through the following code I have added comments to try and help:
-- Variables
local Area = script.Parent
local build = false
local gun1 = game.ServerStorage.gun
-- Main
Area.Touched:Connect(function(hit)
if hit.Parent:FindFirstChildWhichIsA("Humanoid") and hit.Name == "HumanoidRootPart" then -- Check that the hit part is a player and the HRP
gun1:Clone().Parent = game.Players:GetPlayerFromCharacter(hit.Parent).Backpack -- Clone the tool
end
end)
-- This is the event for detecting when players leave the area.
Area.TouchEnded:Connect(function(hit)
-- Detect if a player has left the area.
if hit.Parent:FindFirstChildWhichIsA("Humanoid") and hit.Name == "HumanoidRootPart" then
if hit.Parent:FindFirstChildWhichIsA("Tool") then
-- Unequip all the players tool to put them inside the backpack.
hit.Parent.Humanoid:UnequipTools()
end
-- Destroy the players gun
game.Players:GetPlayerFromCharacter(hit.Parent).Backpack.gun:Destroy()
end
end)
game.StarterPack.gun1 doesn’t work, as your trying to clone it to the player.
instead, you should have your player variable as your GetPlayerFromCharacter function, then from there, preform the necessary checks to ensure that there is a player, then clone the tool.
local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- Get player
if player then -- Check if it is a player
game.ServerStorage.gun1:Clone().Parent = player.Backpack -- if it is, then clone the tool
end