Randomized Tool after death for a certain amount of time

Hello!

I want to make a script that when you die, it gives you a random set of tools from three folders: Melee,Ranged, and extra. But i only want this to happen for a certain amount of time.

Any help appreciated as i do not know how to even start making this sort of script. (The folders would be placed in lighting.)

[Don’t use Lighting!]
It is not advised to store non-lighting objects inside of the LightingService. This is a deprecated practice that has been superseded by services such as ReplicatedStorage and ServerStorage.

I recommend storing tools inside of ServerStorage to prevent exploiting and unnecessary memory load.

IF you were to place the folders in ReplicatedStorage, exploiters would easily be able to grab the tools with a single line of code. As for the memory load, ReplicatedStorage would load the tools on both the client and the server (which is unnecessary, we only need to load them on the server).

[Scripting]
As for the coding itself, it should be quite simple.
Simply create a server script stored inside ServerScriptService and name it whatever.

Your post mentioned that you want to give players tools on death (that would mean as soon as they die they receive the tools, and then lose them due to respawn), but I assume you meant you want them to have new tools on respawn.

To do this we need to use an event to listen for character respawn.

game.Players.PlayerAdded:Connect(function(plr) -- listen for player join
    plr.CharacterAdded:Connect(function(char) -- listen for character spawn

    end)
end)

It’s that simple! Just listen for player join, then attach a listener to the player to detect when their character spawns. This function will only run when the character spawns. (Including when the player joins the game and their character spawns for the 1st time.)

Now for adding the tools to the player.

local SS = game:GetService("ServerStorage") -- where we stored our tools folder
local tools = SS:WaitForChild("Tools") -- the name of our tools folder

--// Give Tool Helper Function //--
local function GiveTool(plr, tool)
    local plrTool = tool:Clone()
    plrTool.Parent = plr.Backpack
    return plrTool
end

--// Get Random Child From Each Child //--
local function GetRandFromEach(folder)
    local randTools = {}
    for _, set in pairs(tools:GetChildren()) do
        local setChildren = set:GetChildren()
        if set:IsA("Folder") and #setChildren  > 0 then -- make sure set is a folder and has children.
            local setRandom = setChildren[math.random(1, #setChildren)] -- grabs random tool from set.
            table.insert(randTools, setRandom)
        end
    end
    return randTools
end

game.Players.PlayerAdded:Connect(function(plr) -- listen for player join
    plr.CharacterAdded:Connect(function(char) -- listen for character spawn
        local tools = GetRandFromEach(tools)
        for _, tool in ipairs(tools) do -- may have to change to 'pairs' instead of 'ipairs'.
            GiveTool(plr, tool)
        end

    end)
end)

This code should work in theory, I wrote this all on the forum so it has not been tested. If it doesn’t work or you have further questions feel free to reply to this!

Hope this helped. :slight_smile:

Does this include a way to get one random item from 3 different folders? also, i apppreciate the advisory to not use lighting. I ask this because i went through the script and didnt see anything with it. Your help is very appreciated.

1 Like

Yes, the GetRandFromEach() function does exactly that.

It loops through every descendant of the tools folder (so it loops through each subcategory) and selects a random tool.

After selecting a tool it adds it to a temporary table (randTools) that gets returned by the function. This table can then be iterated upon, and by using GiveTool() we clone each tool in the table and give it to the player.

Edit: i decided to go ahead and mark you solution as i think i have a solution to my problem. Thanks.

1 Like