Hello, I'm trying to make a simple function where, when the TextButton is clicked, it will cause the player to jump. However, upon clicking it, nothing happens. I made it print something upon clicking it, and it prints, but doesn't make the player jump. I'm not sure what's wrong with it.
button = script.Parent mobile = game:GetService('UserInputService').TouchEnabled if mobile == true then button.Visible = true end? function jump() game.Players.LocalPlayer.Character.Humanoid.Jump = true end button.MouseButton1Down:Connect(jump)
It's also a button that's only meant to display on mobile, which also doesn't work. However, after removing that portion of the script, nothing happens still. What's the problem?
Not sure why they will not jump. You can try to use a RemoteEvent instead. On the server side, listen for the OnServerEvent
event, and on the client side call :FireServer()
.
local debounces = {} -- # list of debounces so player does not spam -- # remember to define remoteEvent remoteEvent.OnServerEvent:Connect(function(client, request) if request == "jump" and not debounces[client] then -- # they requested to jump, and are not in the debounces table debounces[client] = true client.Character.Humanoid.Jump = true wait(2) debounces[client] = nil end end)
And from the local script
local button = script.Parent -- # localise variables local isMobile = game:GetService('UserInputService').TouchEnabled button.Visible = isMobile -- # TouchEnabled holds a boolean value. Just set the visibility to that boolean! local function jump() remoteEvent:FireServer("jump") -- # remember to define remoteEvent end button.Activated:Connect(jump) -- # Activated is the recommended way of listening for GUI button clicks
Also TouchEnabled
should not be the only way to determine if the player is on mobile or not. What if a user has a touch screen computer? You can use MouseEnabled
and KeyboardEnabled
in addition to TouchEnabled
. As well as other checks.
So while Incappax is right that you need to use Remote Events, the following can just be placed into the script without writing extra lines of code.
Local Script
local button = script.Parent local mobile = game:GetService('UserInputService').TouchEnabled local remote = game.ReplicatedStorage.Jumping if mobile == true then button.Visible = true end function jump() remote:FireServer() end button.Activated:Connect(jump)
Server Script
local remote = game.ReplicatedStorage.Jumping remote.OnServerEvent:Connect(function(player) local char = game.Workspace:FindFirstChild(player.Name) if char ~= nil then char.Humanoid.Jump = true end end)
You can also a debounce with a wait time to prevent further use of the button until the player stops jumping.
Local Script with Debounce:
local button = script.Parent local mobile = game:GetService('UserInputService').TouchEnabled local remote = game.ReplicatedStorage.Jumping local debounce = true local player = game.Players.LocalPlayer if mobile == true then button.Visible = true end function jump() if debounce == false then return end debounce = false remote:FireServer() repeat wait() until player.Character.Humanoid.Jump == false debounce = true end button.Activated:Connect(jump)