Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Is there a way to do a check for something every 30 seconds?

I've got a script that will change your tool when you press a button, and it then goes up and down in order of your tools. However, I ran into the problem that if you pick up a new tool, then the new tool will not be included in the list that has the children of the users backpack. Here's the script:

local lpress = 0
local rpress = 0

userInputService.InputBegan:connect(function(input, processed)
	if input.UserInputType == Enum.UserInputType.Gamepad1 then
		if input.KeyCode == Enum.KeyCode.DPadUp then
			game.Players.LocalPlayer.Character.Humanoid:UnequipTools()
			lpress = 0
			rpress = 0
		end
	end
end)

local tools = game.Players.LocalPlayer.Backpack:GetChildren()
local items = {tools[1], tools[2], tools[3], tools[4]}
userInputService.InputBegan:connect(function(input, processed)
	if input.UserInputType == Enum.UserInputType.Gamepad1 then
		if input.KeyCode == Enum.KeyCode.DPadLeft then
			if lpress == 0 then
				game.Players.LocalPlayer.Character.Humanoid:EquipTool(items[10])
				lpress = lpress + 1
				rpress = 10
			elseif lpress == 1 then
				game.Players.LocalPlayer.Character.Humanoid:EquipTool(items[9])
				lpress = lpress + 1
				rpress = 9
			elseif lpress == 2 then
				game.Players.LocalPlayer.Character.Humanoid:EquipTool(items[8])
				lpress = lpress + 1
				rpress = 8
			elseif lpress == 3 then
				game.Players.LocalPlayer.Character.Humanoid:EquipTool(items[7])
				lpress = lpress + 1
				rpress = 7
			elseif lpress == 4 then
				game.Players.LocalPlayer.Character.Humanoid:EquipTool(items[6])
				lpress = lpress + 1
				rpress = 6
			elseif lpress == 5 then
				game.Players.LocalPlayer.Character.Humanoid:EquipTool(items[5])
				lpress = lpress + 1
				rpress = 5
			elseif lpress == 6 then
				game.Players.LocalPlayer.Character.Humanoid:EquipTool(items[4])
				lpress = lpress + 1
				rpress = 4
			elseif lpress == 7 then
				game.Players.LocalPlayer.Character.Humanoid:EquipTool(items[3])
				lpress = lpress + 1
				rpress = 3
			elseif lpress == 8 then
				game.Players.LocalPlayer.Character.Humanoid:EquipTool(items[2])
				lpress = lpress + 1
				rpress = 2
			elseif lpress == 9 then
				game.Players.LocalPlayer.Character.Humanoid:EquipTool(items[1])
				lpress = lpress + 1
				rpress = 1
			elseif lpress == 10 then
				game.Players.LocalPlayer.Character.Humanoid:UnequipTools()
				lpress = 0
				rpress = 0
			end
		end			
	end
end)

userInputService.InputBegan:connect(function(input, processed)
	if input.UserInputType == Enum.UserInputType.Gamepad1 then
		if input.KeyCode == Enum.KeyCode.DPadRight then
			if rpress == 0 then
				game.Players.LocalPlayer.Character.Humanoid:EquipTool(items[1])
				rpress = rpress + 1
				lpress = 10
			elseif rpress == 1 then
				game.Players.LocalPlayer.Character.Humanoid:EquipTool(items[2])
				rpress = rpress + 1
				lpress = 9
			elseif rpress == 2 then
				game.Players.LocalPlayer.Character.Humanoid:EquipTool(items[3])
				rpress = rpress + 1
				lpress = 8
			elseif rpress == 3 then
				game.Players.LocalPlayer.Character.Humanoid:EquipTool(items[4])
				rpress = rpress + 1
				lpress = 7
			elseif rpress == 4 then
				game.Players.LocalPlayer.Character.Humanoid:EquipTool(items[5])
				rpress = rpress + 1
				lpress = 6
			elseif rpress == 5 then
				game.Players.LocalPlayer.Character.Humanoid:EquipTool(items[6])
				rpress = rpress + 1
				lpress = 5
			elseif rpress == 6 then
				game.Players.LocalPlayer.Character.Humanoid:EquipTool(items[7])
				rpress = rpress + 1
				lpress = 4
			elseif rpress == 7 then
				game.Players.LocalPlayer.Character.Humanoid:EquipTool(items[8])
				rpress = rpress + 1
				lpress = 3
			elseif rpress == 8 then
				game.Players.LocalPlayer.Character.Humanoid:EquipTool(items[9])
				rpress = rpress + 1
				lpress = 2
			elseif rpress == 9 then
				game.Players.LocalPlayer.Character.Humanoid:EquipTool(items[10])
				rpress = rpress + 1
				lpress = 1
			elseif rpress == 10 then
				game.Players.LocalPlayer.Character.Humanoid:UnequipTools()
				rpress = 0
				lpress = 0
			end
		end
	end
end)

1 comment

0

I need it so it runs a new check every 30 seconds, and then changes the list "tools" and then ultimately the list "items".

1 answer

Just stick this at the end of all the code.

while wait(30) do
	tools = game.Players.LocalPlayer.Backpack:GetChildren()
	items = {tools[1], tools[2], tools[3], tools[4]}
end

That should be all you need.

4 comments

0

Thanks.

0

Anytime.

0

Sorry but this does work but every time I change my tool by pressing the DPad it changes, but then when it updates it messes it all up. Sorry!

0

What do you mean? Every time the code I wrote changes the table, it causes a problem in the game?

This discussion is preserved as it was. New answers, comments, and votes are closed.