Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Simple sword giver, serverstorage to player backpack?

Asked by
pie9909 18
4 years ago

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?

01local debounce = false
02 
03game.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
20end)
0
Index Backpack, don't use FindFirstChild() on it. DeceptiveCaster 3761 — 4y
0
As in using the i, v table thing? could you show me an example? pie9909 18 — 4y

1 answer

Log in to vote
2
Answered by 4 years ago

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
02local ServerStorage = game:GetService("ServerStorage")
03local Players = game:GetService("Players")
04-- Parts
05local sword = ServerStorage:FindFirstChild("ClassicSword")
06-- Booleans
07local debounce = false
08-- Connections
09game.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
View all 21 lines...
0
Let me know how it goes! DevMaster333 215 — 4y
0
Deleted my last comment, big typeo , but anything that involves the player, i will need GetService? pie9909 18 — 4y
0
You can create a variable called Player and store all the players in that variable. You can get all players using get service or simply game.workspace.Players DevMaster333 215 — 4y
0
OHH okay! that makes a lot more sense. thanks a lot! pie9909 18 — 4y
0
You're welcome! DevMaster333 215 — 4y
Ad

Answer this question