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

how do i keep the sword in the inventory?

Asked by 3 years ago
Edited 3 years ago

I made a system when you kill an NPC you get a sword, but when you die it disappears from the inventory

[NPC Reward]

local Enemy = script.Parent.Humanoid
local RS = game:GetService("ReplicatedStorage")
local event = RS:WaitForChild("Event").Drop

function KillForXp() 
    local tag = Enemy:findFirstChild("creator") 
    if tag ~= nil then 
        if tag.Value ~= nil then 
            event:Fire(tag.Value)
        end 
    end 
end

Enemy.Died:connect(KillForXp) 

ServerScriptService [DropSystem]

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = ReplicatedStorage:WaitForChild("Event").Drop

event.Event:Connect(function(player)
    local drop = {"None", "None", "None", "None", "Sword", "Sword", "Sword", "Red Sword", "Red Sword", "Rare Sword", "None", "None", "None", "None", "None", "None",}
    local randomDrop = drop [math.random(1, #drop)]

    if randomDrop == "Red Sword" then
        local Redsword = script.BaconSword:Clone()
        Redsword.Parent = player.Backpack
    end
end)

[BackPackSave]

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("BackpackSave")

game.Players.PlayerAdded:Connect(function(player)
    pcall(function()
        local tool = dataStore:GetAsync("User-"..player.UserId)
        if tool then
            for i,v in pairs (tool) do
                local toolFound = game.ServerScriptService.DropSystem:FindFirstChild(v)
                if toolFound then
                    toolFound:Clone().Parent = player.Backpack
                    toolFound:Clone().Parent = player.StarterGear
                end
            end
        end
    end)
end)

game.Players.PlayerRemoving:Connect(function(player)
    pcall(function()
        local toolsSave = {}
        for i, tool in pairs(player.Backpack:GetChildren()) do
                if tool then
                    table.insert(toolsSave,tool.Name)
                end

            end
            dataStoreService:SetAsync("User-"..player.UserId,toolsSave)
    end)
end)
0
I would need to see the script PufferfishDev 49 — 3y
0
ok XxDragonCraftersXx 2 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

I managed to solve it, I created another script to save the items

Ad

Answer this question