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.
Put all your items into a folder called "Items" in replicated storage, then run this code inside server script service.
01 | local players = game:GetService( "Players" ) |
02 | local items = game.ReplicatedStorage:WaitForChild("Items) -- You can use server storage, it is just that the client cant access server storage. |
03 |
04 |
05 | players.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 ) |
17 | end ) |
This code is untested. But the thought behind it is that when a character spawns, it gives them a random item.
I'll try to give you a script to work out.
01 | local ServerStorage = game:GetService( "ServerStorage" ) |
02 |
03 | -- Create a folder in ServerStorage and put the tools in there and name it ToolsFolder |
04 | local ToolFol = ServerStorage:FindFirstChild( "ToolsFolder" ) |
05 | local Tools = ServerStorage.ToolsFolder:GetChildren() |
06 |
07 | game.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) |
Try that and if it doesn't work, please comment below!