Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

GUI Jump Button: When clicked, nothing happens?

Asked by 5 years ago

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?

0
well for one, there's a question mark after your end on line 6. if that still doesnt solve your problem, try changing line 9 to: game.Players.LocalPlayer.Character.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping,true) Gey4Jesus69 2705 — 5y
0
for some reason, a while back, setting the jump property to true worked in studio, but not in real servers. using SetStateEnabled worked the opposite however Gey4Jesus69 2705 — 5y
0
@gioni01, I tried what you said and it didnt work in studio, so i posted an answer for what does work in studio, although your way might work on actual servers, idk SerpentineKing 3885 — 5y
0
Whoops. The question mark must have been added while I was typing out the question. It's not in the actual script. Unfortunately, changing line 9 didn't work, either, though. SuperJumpman12 43 — 5y

2 answers

Log in to vote
2
Answered by 5 years ago

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.

0
-rep unnecessary and i dont like u Gey4Jesus69 2705 — 5y
0
shut up mattscy is mine now gisoni01 User#24403 69 — 5y
Ad
Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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)
0
Why get the character from workspace. That is prone to error... User#24403 69 — 5y
0
Also where is the "simplification" ? All you did was remove the whole request. User#24403 69 — 5y
0
you dont need the request, and finding the child does the exact same thing you did except without calling character directly SerpentineKing 3885 — 5y
0
Where do you "call" character. Also player.Character is safer. Nothing else will be the character. What if someone names themself to something in your workspace, either maliciously or unintentionally? Also my remote event can be used for more things. User#24403 69 — 5y
View all comments (9 more)
0
Also could you please stop trying to take credit for my answers. My answer was perfectly fine, no 'simplification' was needed. User#24403 69 — 5y
0
The character is called "char" in my function; Well technically i was already writing this post when you posted so im not really taking credit, i just added a line of info  SerpentineKing 3885 — 5y
0
"so what"? You're at this point begging for your scripts to be broken. Also you implied the character itself is being called when you are referring to the variable name.?? User#24403 69 — 5y
0
"So while Incappax is right that you need to use Remote Events, I'm going to simplify the script a bit." Doubt it. User#24403 69 — 5y
0
its a local variable thats looking for the character, i dont see the problem with that, and i honestly dont just expect everyone to be attempting to break scripts on purpose anytime they enter a game. Besides, i could just add a line to check if char is nil and then not run the code, and tada, no hacks SerpentineKing 3885 — 5y
0
You are assuming everyone who joins is a little angel. Someone might spend 1000 robux just to break your game. People are crazy. Also I cannot believe you think storing the character in a variable makes such a difference. User#24403 69 — 5y
0
Alright wise guy, go find me someone who pays 1000 robux to break games then, and i added the line, so now it wont break if the player leaves before the function fires SerpentineKing 3885 — 5y
0
That makes no difference LMAO. User#24403 69 — 5y
0
ur both retards shut the heck up Gey4Jesus69 2705 — 5y

Answer this question