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

How do i make it so that this drop system only gives the item to a specific player?

Asked by
Sorukan 240 Moderation Voter
5 years ago
Edited 5 years ago

This is a server script and what i'm trying to accomplish is, once an npc has been killed it will drop a part. Once the part has been picked up it will give them an item, in this case a sword i'm not sure how to do it but this was my attempt. The sword just doesn't clone into the Backpack.

--//Services
local serverStorage = game:GetService('ServerStorage')

--//Variables
local weapons = serverStorage:WaitForChild('Weapons')
local myHum = script.Parent:WaitForChild('Humanoid')
local myHead = script.Parent:WaitForChild('Head')

--//Died
myHum.Died:Connect(function()
    local dropChance = math.random(0,3)
    local itemChance = math.random(0,5)

    if dropChance == 3 or 2 or 1 then
        local p = Instance.new('Part')
        p.Anchored = true
        p.CanCollide = false
        p.Size = Vector3.new(3,3,3)
        p.Position = myHead.Position
        p.Parent = game.Workspace

        p.Touched:Connect(function(hit)
            if hit.Parent:FindFirstChild('Humanoid') and hit.Parent.Name ~=     script.Parent.Name then
                local clone = weapons:WaitForChild('Sword'):Clone()
                clone.Parent = hit.Parent:FindFirstChild('Backpack')
                p:Destroy()
            end
        end)
    end
end)
0
serverStorage should be game.ServerStorage also shouldnt hit.Parent.Name and script.Parent.Name have something between them? 129Steve129 7 — 5y

1 answer

Log in to vote
1
Answered by
hellmatic 1523 Moderation Voter
5 years ago
Edited 5 years ago

Backpack is a child of the player object. You were trying to find it under the player's character. You can get the player using GetPlayerFromCharacter:

p.Touched:Connect(function(hit)
    local parentModel = hit.Parent
    if parentModel then 
        local player = game.Players:GetPlayerFromCharacter(parentModel)
        if player then 
            local weapon = weapons:WaitForChild("Sword"):Clone()
            weapon.Parent = player.Backpack
            p:Destroy()
        end
    end
end)
0
Oh, thanks! Sorukan 240 — 5y
Ad

Answer this question