Hi I attempted a simple sword giver script, the script isn't sending the sword to my backpack, but it will send it to the workspace if I change it, am I missing something important?
local debounce = false game.Workspace.Part.Touched:Connect(function() if not debounce then debounce = true print("test") local cloneSword = game.ServerStorage.ClassicSword:Clone() cloneSword.Parent = game.Players:FindFirstChildOfClass("Backpack") wait(1) debounce = false end end)
What you need to do first is detect if the part that hit the object is actually a character. Next, you would get the character's player to get his backpack and finally clone and parent the item to the player's backpack.
Here's how you would do it:
-- Services local ServerStorage = game:GetService("ServerStorage") local Players = game:GetService("Players") -- Parts local sword = ServerStorage:FindFirstChild("ClassicSword") -- Booleans local debounce = false -- Connections game.Workspace.Part.Touched:Connect(function(hit) -- Check if the part that hit is the part of a character local char = hit.Parent local hum = char:FindFirstChild("Humanoid") if hum and not debounce then debounce = true local player = Players:GetPlayerFromCharacter(char) local swordClone = sword:Clone() swordClone.Parent = player.Backpack debounce = false end end)