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

How to make a Gui show up if the player hasn't moved the mouse for ten seconds?

Yes, sorry for all the questions, but I really need your help. I need this script to make a Gui visible if times == 0, but the script just doesn't do anything.

player = game.Players.LocalPlayer
mouse = player:GetMouse()
times = 10

mouse.Move:connect(function()
player.PlayerGui.Screensaver.Main.Visible = false
times = 10
end)
while true do
	wait(1)
	times = times - 1
	print("Screenz:".. times)
end
while true do
	if times == 0 then
		player.PlayerGui.Screensaver.Main.Visible = true
		wait(1)
	else
		print("ScreenSaver Not Active, time waited is not enough.")
	end
end

--Thanks in Advance!

3 answers

Here is what I did. I made comments so you know what each part does. Also, you can make the while loop run under a certain condition if you did not know. This will stop a lot of lag im pretty sure. Also, you can use the Idle event that detects if the mouse has not moved, or pressed a button. The debounces are just so that the wait functions actually have a use, and that it counts in seconds, not milliseconds.

local times = 10
local player = game.Players.LocalPlayer
local debounce = false
local debounce2 = false
game.Players.LocalPlayer:GetMouse().Idle:connect(function() --Fires when the mouse is idle and when times is not 0.
	if debounce == false and times ~= 0 then
		debounce = true
		wait(1)
		times = times - 1
		print("Screenz:".. times)
		debounce = false
		debounce2 = false --Makes the movement detecting function run again since the debounce is false.
	end
end)

game.Players.LocalPlayer:GetMouse().Move:connect(function() --Fires when the mouse is moving and when times is not 10
	if debounce2 == false and times ~= 10 then
		debounce2 = true
		times = 10
		player.PlayerGui.Screensaver.Main.Visible = false
	end
end)

while times == 0 do --Fires when times is 0.
	wait()
	player.PlayerGui.Screensaver.Main.Visible = true
	break --Breaks the loop so it reduces lag.
end

2 comments

0

Other than using a loop and not putting that argument the loop is waiting for in a separate if gate within the idled connection. This is pretty good. However he shouldn't use a loop for that.. When the mouse idles you are running the idles connection AND the loop. Thats inefficient.

0

No, the loop is not running. It only runs if the condition(times == 0) is true.

First let's create a function that will run when they haven't moved in 10 seconds and debounce variable

local d = true

function runthis()
	print('hi')
end

Next let's create a variable for the seconds and mouse Move event

local lasttime = 0
local mouse = game.Players.LocalPlayer:GetMouse()

mouse.Move:connect(function()
	lasttime=0
	d=true
end)

now for the changing the lasttime variable and check if its over 10

while true do
	if lasttime>10 then
		d=false
		runthis()
	end
	if d==true then
		lasttime=lasttime+1
		wait(1)
	end
end

Let's put it together

local d = true

function runthis()
	print('hi')
end

local lasttime = 0
local mouse = game.Players.LocalPlayer:GetMouse()

mouse.Move:connect(function()
	lasttime=0
	d=true
end)

while true do
	if lasttime>10 then
		d=false
		runthis()
	end
	if d==true then
		lasttime=lasttime+1
		wait(1)
	end
end
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local move = tick()

mouse.Move:Connect(function()
	move = tick()
end)

while true do
	wait()
	if tick() - move >= 10 then
		print("10 seconds")
	-- tried to add break() here, but it wont run it again when the player moves the mouse, after 10 seconds
	end
end

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