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?
01 | local debounce = false |
02 |
03 | game.Workspace.Part.Touched:Connect( function () |
04 |
05 | if not debounce then |
06 |
07 | debounce = true |
08 |
09 | print ( "test" ) |
10 |
11 | local cloneSword = game.ServerStorage.ClassicSword:Clone() |
12 |
13 | cloneSword.Parent = game.Players:FindFirstChildOfClass( "Backpack" ) |
14 |
15 | wait( 1 ) |
16 |
17 | debounce = false |
18 |
19 | end |
20 | 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:
01 | -- Services |
02 | local ServerStorage = game:GetService( "ServerStorage" ) |
03 | local Players = game:GetService( "Players" ) |
04 | -- Parts |
05 | local sword = ServerStorage:FindFirstChild( "ClassicSword" ) |
06 | -- Booleans |
07 | local debounce = false |
08 | -- Connections |
09 | game.Workspace.Part.Touched:Connect( function (hit) |
10 | -- Check if the part that hit is the part of a character |
11 | local char = hit.Parent |
12 | local hum = char:FindFirstChild( "Humanoid" ) |
13 |
14 | if hum and not debounce then |
15 | debounce = true |