XP Levelbar issues on mobile and console

I have a game where players jump to gain JumpPower and XP, which is activated by ‘MouseButton1Down’. Each time the player jumps, points are added (JumpPower and XP).

XP is visually shown in the UI as a level bar that increases as the player gains XP until the next level is reached.
Screen Shot 2020-01-15 at 5.39.52 PM

On PC everything works, but on mobile and console, the level bar does not fill up (I do not have a console to test it, but got bug reports from players).

What am I doing wrong? I do not seem to find the problem (there is no error message).

I am not sure how to go about fixing this. Any input or pointers would be much appreciated

This is my code:
–******–
–// in StarterPlayer.StarterPlayerScripts

local plr = game.Players.LocalPlayer
local Remote = workspace.Events.AddPoints
local Debounce = true
local Char = plr.Character or plr.CharacterAdded:Wait()
local Key = 'MouseButton1Down'
local mouse = plr:GetMouse()

mouse.KeyDown:Connect(function(key)
    if key:byte() == 32 then
 		if Debounce and Char and Char.Humanoid.Jump == false then
			Debounce = false
  			Remote:FireServer()
		end
			
end
wait(2)
 			Debounce = true
end)

–******–
–// event placed in a script in workspace
script.AddPoints.OnServerEvent:Connect(function(plr)

	if plr.PlayerStats.Rebirths.Value > 1 then
		if plr.PlayerStats.Level.Value > 1 then
			plr.PlayerStats.JumpPower.Value = plr.PlayerStats.JumpPower.Value + (.2 *(plr.PlayerStats.Rebirths.Value+plr.PlayerStats.Level.Value))
		else
			plr.PlayerStats.JumpPower.Value = plr.PlayerStats.JumpPower.Value + (.2 *(plr.PlayerStats.Rebirths.Value))
		end
	else
		if plr.PlayerStats.Level.Value > 1 then
			plr.PlayerStats.JumpPower.Value = plr.PlayerStats.JumpPower.Value + (.2+plr.PlayerStats.Level.Value)
			
		else
			plr.PlayerStats.JumpPower.Value = plr.PlayerStats.JumpPower.Value + (.2)
		end
		
	end
	
	if plr.PlayerStats.DoubleJump.Value == true then
		if plr.PlayerStats.Rebirths.Value > 1 then
			if plr.PlayerStats.Level.Value > 1 then
				plr.PlayerStats.JumpPower.Value = plr.PlayerStats.JumpPower.Value + (.2 *plr.PlayerStats.Rebirths.Value)+plr.PlayerStats.Level.Value
			else
				plr.PlayerStats.JumpPower.Value = plr.PlayerStats.JumpPower.Value + (.2 *(plr.PlayerStats.Rebirths.Value))
			end
			else
		if plr.PlayerStats.Level.Value > 1 then
			plr.PlayerStats.JumpPower.Value = plr.PlayerStats.JumpPower.Value + (.2+plr.PlayerStats.Level.Value)
			
		else
			plr.PlayerStats.JumpPower.Value = plr.PlayerStats.JumpPower.Value + (.2)
		end
		
	end
	end
	plr.PlayerStats.XP.Value = plr.PlayerStats.XP.Value +1
end)

–******–
–// in StarterGUI, local script

local function levelBarDisplay ()
	--//Display Levelbar progress
        --// script credit for level bar: @Elttob
	local xp = XP.Value
	local level = Level.Value
	local Base = interface.Toggles.Trackers.Level.Base -- change this to refer to your Base ImageLabel
	local oneOverProgress
	local total 
	if level == 0 then total = 50 else total = level*50 end
	local progress = xp/total
    if progress == 0 then
        oneOverProgress = 0
    else
        oneOverProgress = 1/progress
    end

		--// Loadingbar-fill
    	Base.Clipping.Size = UDim2.new(progress, 0, 1, 0) 
    	Base.Clipping.Top.Size = UDim2.new(oneOverProgress, 0, 1, 0)
	
		wait()
end

levelBarDisplay()

-- XP increase displays change in tracker bar
plr.PlayerStats.XP.Changed:Connect(function()
	levelBarDisplay() 
end)

Do you own a console / mobile device yourself that you can test it on? if so i’d suggest debugging to see where it stops working

Developers are recommended to use UserInputService instead of the mouse object in new work. I would recommend listening for when the player jumps using PlayerActions Enum in your StarterPlayerScript. Also you should always use WaitForChild() in StarterPlayerScripts when referncing to objects outside of ReplicatedFirst service (your remote). It could be that the remote is not firing because the script errored when player join.

1 Like

I only have a mobile to test on, no console. It looks like on the mobile, the code is read inside the mouse.KeyDown:Connect(), but the event never fires, even though the player jumps (using the defat jump button on mobile).

@corotines mentioned using UserInputService and the PlayerActions Enum. I’ll look into that and report back.

1 Like

I have changed my code to this:

local plr = game.Players.LocalPlayer
local Events = workspace:WaitForChild("Events")
local Remote = Events:WaitForChild("AddPoints")
local UIS = game:GetService("UserInputService")
local Debounce = true
local Char = plr.Character or plr.CharacterAdded:Wait()
local Key = 'MouseButton1Down'
local mouse = plr:GetMouse()

UIS.InputBegan:Connect(function(Input)
	print("inside jump")
	local KeyCode = Input.KeyCode
	if KeyCode == Enum.KeyCode.Space then
		
 		if Debounce and Char and Char.Humanoid.Jump == false then
			Debounce = false
  			Remote:FireServer()
		end
			
end
wait(2)
 			Debounce = true
	
end)

I saw that Enum for PlayerAction “jump” is “4”, but I’m not sure how to apply that yet in the code. It’s my first time using UserInputService. The above code works on PC the same way as did my previous code. I added print statements and saw that the remote event never gets fired on mobile, but the code is read inside the UserInputBegan.

-- StarterPlayer.StarterCharacterScripts
local Workspace = game:GetService("Workspace")
local Humanoid = script.Parent:WaitForChild("Humanoid")
local Remote = Workspace:WaitForChild("Events"):WaitForChild("AddPoints")

Humanoid.Jumping:Connect(function(active)
	if active then
	Remote:FireServer()
	end
end)

Note, I suggest putting your remotes in ReplicatedStorage to be more organized.

1 Like

Thank you! Your help is very much appreciated. It works perfect for mobile now and I’m assuming for console as well (just waiting to hear back).For now, I’ll mark this is solved.

I’ll work on keeping my code more organized in the future.

1 Like

I had another question regarding the script. It works for mobile and tablets, which is amazing, but now the player can just hold down the space bar or jump button on mobile and points get added automatically instead of having to click for each jump. Now the players do auto-clicking. I’ve tried to break out of the function but it doesn’t work.

This is what I have atm:

local Workspace = game:GetService("Workspace")
local Humanoid = script.Parent:WaitForChild("Humanoid")
local Remote = Workspace:WaitForChild("Events"):WaitForChild("AddPoints")
local Debounce = true

Humanoid.Jumping:Connect(function(active)
	if active then
		if Debounce then
			Remote:FireServer()
			Debounce = false
		end
	end
	
	wait(2)
 	Debounce = true
	
end)