How to make this script refresh after X time?

I want to make this tool detection script refresh after 30-60 seconds but i don’t know how to do this, while true do din’t worked

local toolName = “PFoodCan”

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		if Player.Backpack:FindFirstChild(toolName) then
			Player.PlayerGui:WaitForChild("Shop").Frame.GiveToolPFood.Text = "Purchased"
			Player.PlayerGui:WaitForChild("Shop").Frame.GiveToolPFood.GiveTool:Remove()
			print("foodCan found")
		else
			print("foodCan not found")
		end
	end)
end)

local toolName2 = "WaterBucket"

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		if Player.Backpack:FindFirstChild(toolName2) then
			Player.PlayerGui:WaitForChild("Shop").Frame.GiveToolWBucket.Text = "Purchased"
			Player.PlayerGui:WaitForChild("Shop").Frame.GiveToolWBucket.GiveTool:Remove()
			print("WaterBucket found")
		else
			print("WaterBucket not found")
		end
	end)
end)

local toolName3 = "SilverCoin"

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		if Player.Backpack:FindFirstChild(toolName3) then
			Player.PlayerGui:WaitForChild("Shop").Frame.GiveToolSilverCoin.Text = "Purchased"
			Player.PlayerGui:WaitForChild("Shop").Frame.GiveToolSilverCoin.GiveTool:Remove()
			print("SilverCoin found")
		else
			print("SilverCoin not found")
		end
	end)
end)

local toolName4 = "GoldCoin"

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		if Player.Backpack:FindFirstChild(toolName4) then
			Player.PlayerGui:WaitForChild("Shop").Frame.GiveToolGoldCoin.Text = "Purchased"
			Player.PlayerGui:WaitForChild("Shop").Frame.GiveToolGoldCoin.GiveTool:Remove()
			print("GoldCoin found")
		else
			print("GoldCoin not found")
		end
	end)
end)
local Tools = {
    PFoodCan = "PFood",
    WaterBucket = "WBucket",
    SilverCoin = "SilverCoin",
    GoldCoin = "GoldCoin"
}

function CheckTools(Player)
    for ToolName, GiveTool in pairs(Tools) do
        if Player.Backpack:FindFirstChild(ToolName) then
            local GiveToolFood = Player.PlayerGui:WaitForChild("Shop").Frame["GiveTool"..GiveTool]

            GiveToolFood.Text = "Purchased"
            GiveToolFood.GiveTool:Destroy()

            print(ToolName.." found")
        else
            print(ToolName.." not found")
        end
    end
end

game.Players.PlayerAdded:Connect(function(Player)
    Player.CharacterAdded:Wait()
        
    while true do
        if Player then
            CheckTools(Player)

            task.wait(Random.new():NextNumber(30, 60))
        else
            break
        end
    end
end)

does this work?

EDIT:
fixed the code a bit

1 Like

Use a while wait(x) do loop.

won’t be much different then a while true do

besides use task.wait()

I replaced :Remove() with :Destroy()
so the code is updated

Thanks!, it works i had changed the name of the item that’s why it wasn’t working

yea in case you need to add more tools
make the index the name of the tool

the value is used for the GiveTool child of Frame
the first one is “PFood”, so the GiveToolFood variable would be equal to Frame.GiveToolPFood

hope this helps out a bit

1 Like