How to give player a random tool every 10 minutes?

How would I give a player a random tool every 10 minutes and them keep it after death but not after rejoining, taking it from a set of tools from ServerStorage and if they do have the tool for it choose another tool to give them?

1 Like

using get in pairs and math random tp determine tool

2 Likes

this doesnt filter out tools the plr alr has; I will write some code that does what he says. give me like 5 - 10 mins

2 Likes

This is a good example to learn the order of your code. Also you might want to look at math.radom(x,y). Also why not put the tools in replciated storage?

1 Like

for the most part this is the code you should need. Im p sure if a player joins while the script is in the middle of waiting, the player wont get a tool until the script is ready to give everyone a new tool. i wasn’t sure if you wanted it to do that or not, so i just didn’t add it. If you did want it though, its easy to implement.

i added as much comments as possible to give u an in-depth understanding on how this works.

this code is what you need for the most part though.

local PlayerService = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local HttpService = game:GetService("HttpService")

local PlrStats = ReplicatedStorage:WaitForChild("PlrStats") --or change the pathway to your liking. It must be a folder or something tho.
local FolderOfTools = ServerStorage:WaitForChild("FolderOfTools") -- change path to ur liking

local Minutes_To_Wait = 10 --change this to ur liking



PlayerService.PlayerAdded:Connect(function(plr)
	
	local StringVal = Instance.new("StringValue",PlrStats)
	StringVal.Name = plr.UserId
	StringVal.Value = HttpService:JSONEncode({}) --basically we are converting an array into a string val to store. We will store names of tools in here.
	
	plr.CharacterAdded:Connect(function(char)
		local List = HttpService:JSONDecode(StringVal.Value)
		if List  == {} then return end
		
		for index,Tool_Name in pairs(List) do
			
			if plr.Backpack:FindFirstChild(Tool_Name) then return end
			
			local Tool_Clone = FolderOfTools:FindFirstChild(Tool_Name):Clone()
			Tool_Clone.Parent = plr.Backpack
			
		end
		
	end)
	
end)


repeat
	task.wait()
until #PlayerService:GetPlayers() >= 1 -- wait for atleast 1 plr to be loaded into the server.

local function GetListOfAvalibleTools(Blacklist)
	local List = {}
	for i,v in pairs(FolderOfTools:GetChildren()) do 
		
		if not table.find(Blacklist,v.Name) then
			
			table.insert(List,v.Name)
			
		end
		
	end
	
	return List
end


while true do 
	
	for index,Player in pairs(PlayerService:GetPlayers()) do
		
		local Id = Player.UserId
		local StatInstance:StringValue = PlrStats:WaitForChild(Player.UserId,1) --wait for the string value instance we put earlier. Max wait is 1 sec.
		
		if not StatInstance then return end --if for some reason the plr doesnt have string val, then we just go to the next plr
		
		local Blacklist = HttpService:JSONDecode(StatInstance.Value) -- we unpack the list of tools the plr already has. Stuff they CANT get anymore.
		
		local Possible_Choices = GetListOfAvalibleTools(Blacklist) -- we get a list of stuff the plr can potentially get.
		
		if #Possible_Choices == 0 then return end --if the plr alr has all the possible tools, we just go to the next plr.
		
		local Selected_Tool_Name = Possible_Choices[math.random(1,#Possible_Choices)] --choose a random tool name
		
		table.insert(Blacklist,Selected_Tool_Name) -- add its name to the blacklist/tools the plr already has
		
		local Tool_Clone = FolderOfTools:FindFirstChild(Selected_Tool_Name):Clone() --find the tool
		Tool_Clone.Parent = Player.Backpack --put the tool in the plrs backpack.
		
		StatInstance.Value = HttpService:JSONEncode(Blacklist) -- update the players blacklist
		
	end
	
	
	task.wait(60 * Minutes_To_Wait)
end

lmk if I didnt make any sense :upside_down_face:

2 Likes

The script seems to work but one thing is that it gives me a free tool for some random reason, its a tool that is in the tools folder. Let’s say I join with two tools in the folder a Green Ballon, Cake tool and for some reason on join I get the cake tool.

Here i made a code that, i think, solves your Question.

So this scripts creates a table with all the tools (u need to add them), then uses a math.random to select one, after that adds to the player inventory.

also the timer is added, so every 10 mins it changes

local toolTable = {}  -- create table to store the tools

-- add tools to the table
table.insert(toolTable, game.ServerStorage.Tool1)
table.insert(toolTable, game.ServerStorage.Tool2)
-- add more tools (if needed)

-- function to select a random tool (1 in table) then give it
local function getRandomTool()
    local index = math.random(1, #toolTable)
    return toolTable[index]
end

local function giveRandomTool(player)
    local tool = getRandomTool()
    if player and player.Character then
        tool:Clone().Parent = player.Backpack
    end
end

local function moveToolFromBackpack(player)
    if player and player.Character and player.Backpack then
        for _, tool in ipairs(player.Backpack:GetChildren()) do
            tool.Parent = player.Character
        end
    end
end

game.Players.PlayerAdded:Connect(function(player)
    -- give initial tool when the player joins
    giveRandomTool(player)

    -- set up repeating timer to give tools every 10 minutes
    local timer
    timer = game:GetService("RunService").Heartbeat:Connect(function()
        if player.Parent == nil then
            -- player left the game, stop the timer
            timer:Disconnect()
        else
            -- give a new tool every 10 minutes
            giveRandomTool(player)
        end
    end)
end)

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        moveToolFromBackpack(player)
    end)
end)

Hope this help you, any questions just ask

1 Like