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?

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)
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:

-- 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)
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