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

how do i give the player a random gear upon respawn?

Asked by 3 years ago

how would i put a random gear from server storage into the players backpack each time they respawn? (a different gear every respawn) i cant code all to well and the most i can do is edit a script, i cant write my own.

0
Try using math random, and then create a line of code where if it generated number 1 it would give the player a tool if it generated number 2 it would give the player a different tool johncrazy1rb 11 — 3y
0
like if and elseif statements but to be more safe, you can also check just incase if the player doesn't have a tool in their character or backpack KixWater 126 — 3y

2 answers

Log in to vote
1
Answered by 3 years ago

Put all your items into a folder called "Items" in replicated storage, then run this code inside server script service.

local players = game:GetService("Players")
local items = game.ReplicatedStorage:WaitForChild("Items) -- You can use server storage, it is just that the client cant access server storage.


players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function() --This also runs every time the player dies.
        local backpack = player.Backpack
        local children = items:GetChildren() --Gets the children

        local random = math.random(1,#children) -- I think at arrays start at 0, so change to 1 or 0 based on if this works or not.

        local child = children[random]

        local clone = child:Clone()
        clone.Parent = backpack
    end)
end)

This code is untested. But the thought behind it is that when a character spawns, it gives them a random item.

0
You have to check if the player has a tool in their character or backpack KixWater 126 — 3y
0
I don't think that there is actually a reason to do so because the character added function only runs once. But I agree that to be more safe you should check if they already have a gear. movementkeys 163 — 3y
Ad
Log in to vote
0
Answered by
KixWater 126
3 years ago
Edited 3 years ago

I'll try to give you a script to work out.

local ServerStorage = game:GetService("ServerStorage")

-- Create a folder in ServerStorage and put the tools in there and name it ToolsFolder
local ToolFol = ServerStorage:FindFirstChild("ToolsFolder")
local Tools = ServerStorage.ToolsFolder:GetChildren()

game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(char)
        local hum = char:WaitForChild("Humanoid")

        hum.Died:Connect(function()
            plr:LoadCharacter()
            local ToolinChar = char:FindFirstChildOfClass("Tool")
            local ToolinBack = plr.BackPack:FindFirstChildOfClass("Tool")
            local rannum = math.random(1, #Tool)

            if ToolinChar then
                ToolinChar:Destroy()

            elseif ToolinBack then
                ToolinBack:Destroy()
            end

            if rannum == 1 then
                local ClonedTool = ToolFol:FindFirstChild("ToolNameInHere")
                ClonedTool.Parent = char.BackPack

            elseif rannum == 2 then
                local ClonedTool = ToolFol:FindFirstChild("ToolNameInHere")
                ClonedTool.Parent = char.BackPack

            elseif rannum == 3 then
                local ClonedTool = ToolFol:FindFirstChild("ToolNameInHere")
                ClonedTool.Parent = char.BackPack

                elseif rannum == 4 then
                local ClonedTool = ToolFol:FindFirstChild("ToolNameInHere")
                ClonedTool.Parent = char.BackPack

                -- Add as many elseif statements for how many tools in the tool folder...
            end

            print("Gave A Player a new Tool On Death!")
        end)
    end)
end)

Try that and if it doesn't work, please comment below!

0
By the way this is a Normal Script and put it in ServerScriptService KixWater 126 — 3y

Answer this question