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 4 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 — 4y
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 — 4y

2 answers

Log in to vote
1
Answered by 4 years ago

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

01local players = game:GetService("Players")
02local items = game.ReplicatedStorage:WaitForChild("Items) -- You can use server storage, it is just that the client cant access server storage.
03 
04 
05players.PlayerAdded:Connect(function(player)
06    player.CharacterAdded:Connect(function() --This also runs every time the player dies.
07        local backpack = player.Backpack
08        local children = items:GetChildren() --Gets the children
09 
10        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.
11 
12        local child = children[random]
13 
14        local clone = child:Clone()
15        clone.Parent = backpack
16    end)
17end)

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 — 4y
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 — 4y
Ad
Log in to vote
0
Answered by
KixWater 126
4 years ago
Edited 4 years ago

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

01local ServerStorage = game:GetService("ServerStorage")
02 
03-- Create a folder in ServerStorage and put the tools in there and name it ToolsFolder
04local ToolFol = ServerStorage:FindFirstChild("ToolsFolder")
05local Tools = ServerStorage.ToolsFolder:GetChildren()
06 
07game.Players.PlayerAdded:Connect(function(plr)
08    plr.CharacterAdded:Connect(function(char)
09        local hum = char:WaitForChild("Humanoid")
10 
11        hum.Died:Connect(function()
12            plr:LoadCharacter()
13            local ToolinChar = char:FindFirstChildOfClass("Tool")
14            local ToolinBack = plr.BackPack:FindFirstChildOfClass("Tool")
15            local rannum = math.random(1, #Tool)
View all 46 lines...

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 — 4y

Answer this question