How to figure out what device the player is currently using

Hello!

So I have two GUI labels that show the leaderstats for mobile players. However, my script that detects if the player is on mobile or not doesn’t seem to be working.

local UserInputService = game:WaitForChild("UserInputService")

local IsMobile = UserInputService.TouchEnabled
local IsConsole = UserInputService.GamepadEnabled
local IsComputer = UserInputService.KeyboardEnabled

local Player = game.Players.LocalPlayer
local leaderstats = Player:WaitForChild("leaderstats")
local Time = leaderstats:WaitForChild("Time")
local Age = leaderstats:WaitForChild("Age")

local TimeLabel = script.Parent:WaitForChild("Time")
local AgeLabel = script.Parent:WaitForChild("Age")

repeat task.wait() until game:IsLoaded()

while task.wait(1) do
	TimeLabel.Text = "Time: " .. Time.Value	
	AgeLabel.Text = "Age: " .. Age.Value
end

if IsMobile == true then
	TimeLabel.Visible = true 
	AgeLabel.Visible = true
elseif IsComputer then
	TimeLabel.Visible = false 
	AgeLabel.Visible = false
elseif IsConsole then
	TimeLabel.Visible = false 
	AgeLabel.Visible = false
end

If you think you know what went wrong, please let me know. Thank you and have a wonderful day! :slight_smile:

1 Like

Have you checked the output for any errors?

3 Likes

There are none. It’s completely empty.

1 Like

Have you tried using print() to make sure the code is even running?

3 Likes

I think I fixed it! The script wasn’t getting past the while task.wait() loop, so I moved it to the bottom of the script and it worked! Thanks for the help, though!

1 Like

Ah yeah, didn’t look at that part of the script. You should consider using GetPropertyChangedSignal() to listen for changes in value objects.

Documentation

3 Likes

How would I use it in this case?

1 Like
Time:GetPropertyChangedSignal("Value"):Connect(function()
	TimeLabel.Text = "Time: " .. Time.Value
end)

Instead of running a while loop every second, you can update the TextLabel right when the value is changed.

3 Likes

That IS a much better way! And I think it will save memory! Thanks for the tip!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.