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

How do I prevent tools from disappearing from the player's backpack after death?

Asked by 4 years ago

I created a shop that sold guns, and it all worked fine. The thing is, when the player dies, they lose all the guns that they bought, and only the starter gun remains in their inventory.

Here's my LocalScript inside of one of the guns:

game.Players.LocalPlayer.Backpack.ChildAdded:Connect(function()
    local MaxAmmo = 40
    local Ammo = MaxAmmo
    local character = game.Players.LocalPlayer.Character
    local function createBullet()
        local bulletPart = workspace.bullet:Clone()
        bulletPart.Parent = workspace
        bulletPart.Position = script.Parent.Handle.Part.Position
        bulletPart.Orientation = bulletPart.Orientation + (script.Parent.Handle.Orientation - Vector3.new(0,90,0))
        for i = 0,25,10 do
            bulletPart.CFrame = bulletPart.CFrame * CFrame.new(0,0,i)
            wait()
        end
        bulletPart:remove()
    end

    local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
    while not character do
        character.AncestryChanged:Wait()
    end

    local hum = character:WaitForChild('Humanoid')
    local reloadAnim = hum:LoadAnimation(script.Parent.Animation)
    local fireAnim = hum:LoadAnimation(script.Parent.fire)

    script.Parent:WaitForChild('MaxAmmo').Value = MaxAmmo
    script.Parent:WaitForChild('Ammo').Value = Ammo

    local canDamage = true
    local Reloading = false
    local uis = game:GetService("UserInputService")

    script.Parent.Equipped:Connect(function()
        uis.MouseIconEnabled = true
        game.Players.LocalPlayer:GetMouse().Icon = 'rbxassetid://68308747'
    end)
    script.Parent.Unequipped:Connect(function()
        uis.MouseIconEnabled = false
        game.Players.LocalPlayer:GetMouse().Icon = 'rbxassetid://442141180'
    end)

    local canFire = true
    local isDamaging = false

    script.Parent.Activated:Connect(function()
        canFire = true
        if Ammo > 0 and not Reloading then
            while canFire == true do
                isDamaging = true
                script.Parent.Handle.fire:Play()
                fireAnim:Play()
                Ammo = Ammo - 1
                script.Parent.Handle.BillboardGui.Frame.TextLabel.Text = Ammo.." / "..MaxAmmo
                script.Parent.Handle.Part.FrontFlash.Transparency = 0
                script.Parent.Handle.Part.BackFlash.Transparency = 0
                wait()
                script.Parent.Handle.Part.FrontFlash.Transparency = 1
                script.Parent.Handle.Part.BackFlash.Transparency = 1
                createBullet()
                if Ammo <= 0 then
                    canFire = false
                end
            end
            while true do
                if isDamaging == true then
                    game.Players.LocalPlayer:GetMouse().Target.Parent:WaitForChild('EnemyHum'):TakeDamage(6)
                end
                wait(.1)
            end
        elseif Reloading == false then
            uis.InputBegan:Connect(function(keyCode)
                if keyCode.keyCode == Enum.KeyCode.R then
                    if Reloading == false then
                        if Ammo <= 0 then
                            reloadAnim:Play()
                            Reloading = true
                            script.Parent.Handle.reload:Play()
                            wait(script.Parent.Handle.reload.TimeLength)
                            script.Parent.Handle.BillboardGui.Frame.TextLabel.Text = "40 / 40"
                            Ammo = MaxAmmo
                            Reloading = false
                        end
                    end
                end
            end)
        end
    end)
    script.Parent.Unequipped:Connect(function()
        isDamaging = false
        canDamage = false
        canFire = false
        Reloading = false
        script.Parent.Handle.reload:Stop()
        reloadAnim:Stop()
    end)
    script.Parent.Deactivated:Connect(function()
        isDamaging = false
        canFire = false
    end)
end)

Yes, this is long and I'm very sorry for that. I really need answers though, because I want this game out as soon as possible.

**

0
do you have a core script for players? just wondering because my method involves cloning the players tools when they die, temporarly store them in a seperate folder, then when the character loads, I take those cloned tools and replace them in the backpack. mantorok4866 201 — 4y

1 answer

Log in to vote
0
Answered by
Nickzus 24
4 years ago
Edited 4 years ago

Hello,

The problem is not with the script in the gun, but with the script in the shop. One thing you can do to solve that is when you clone the gun part to the backpack of the player, you will need to also clone it to the starter gear, thus the item will persist when your player dies:


local gun1 = game.ServerStorage.gun:Clone() local gun2 = game.ServerStorage.gun:Clone() gun1.Parent = player:FindFirstChild("Backpack") gun2.Parent = player:FindFirstChild("StarterGear")

Sorry, I forgot to mention that you will need to create another global script in the server script storage like that:

game.Players.PlayerAdded:Connect(function(plr)
    local StarterGear = Instance.new("Folder")
    StarterGear.Name = "StarterGear"
    StarterGear.Parent = plr

    plr.CharacterAdded:connect(function()
        local startItems = StarterGear:GetChildren()
        for i=0,#startItems do
            local itemToClone = startItems[i]:Clone()
            itemToClone.Parent = plr.Backpack
        end 
    end)    
end)
0
It doesn't work! It just says that StarterGear doesn't exist..? Sensei_Developer 298 — 4y
0
Try it now Nickzus 24 — 4y
Ad

Answer this question