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

Run GUI works during ''Play'' test, but doesn't in ROBLOX Player...?

Asked by 5 years ago
Edited 5 years ago

Lately, I have tried to make to make a GUI. It is located in Startergui. By clicking on a text button, it's text changes. Inside the text button is a script that changes the player's speed depending on what text is present. This is the script itself :


function run()
 if script.Parent.Text == "Dash"
  then script.Parent.Parent.Parent.Parent.Character.Humanoid.WalkSpeed = 70
  script.Parent.Text = "Walk"
 elseif script.Parent.Text == "Walk"
  then script.Parent.Parent.Parent.Parent.Character.Humanoid.WalkSpeed = 25
  script.Parent.Text = "Dash" 
 end
end

script.Parent.MouseButton1Click:connect(run)


In others words : If the button reads ''Dash'', you gain a walkspeed of 70 If the button reads ''Walk'', you gain a walkspeed of 25. When clicking said button while Play Testing inside the studio, it works like a charm.

However, when inside a game, clicking the button doesn't affect the player's speed!

I've thought that either the click didn't register, or the script didn't work, but it is most likely the latter. It is not a LocalScript, just a regular script. I haven't tried to come up with a solution, due to lack of knowledge. I would be kind if anyone we're to help me fix this script! It's pretty much a key element in my game!

0
When you make your question, put your code in between the Code Block so we can read it. Pojoto 329 — 5y
0
Fixed. Spacejambox 0 — 5y
0
Thanks! :) Pojoto 329 — 5y
0
Did you try to putting it in a local script? That's one of the only possible solutions I am thinking of right now. Also, keep in mind that in a game the server-side and client is separated, unlike the solo test. Other then that, your code appears correct. RAYAN1565 691 — 5y

2 answers

Log in to vote
0
Answered by
Pojoto 329 Moderation Voter
5 years ago
Edited 5 years ago

First off, ALWAYS USE LOCAL SCRIPTS IN GUI!!! :O

When you want a player's GUI to change, you don't want the every other player's GUI to change too right? That's why we use local scripts located in GUI, they only change what's local, on the current player only.

The reason why it worked during play test was because Roblox Studio acts as the server and client(player) and that's why it's not always accurate to play test things.

The solution is fairly simple, just change your script to a Local Script.

However, I also recommend you use variables. This makes it so you don't have to type script.Parent.Parent.Parent.Parent.Parent.Parent on and on and on every time you want to reference that object.

I hope I helped! :)

1
using a Script would just not run. User#19524 175 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

"It is not a LocalScript, just a regular script." Then of course it does not work. Scripts do not run in PlayerGui. They will run from Play Solo, and in Experimental Mode. However, with Experimental Mode being removed, this is no longer the case and Scripts will no longer run from there in a normal server. The fix would be to use a LocalScript. These in fact do run in PlayerGui, and these should be used to handle GUI input and any other input.

local player = game.Players.LocalPlayer -- only works from localscripts
-- avoid using script.Parent.Parent.Parent.Parent.Parent.Parent.Parent

local function run() -- use local functions!
    if script.Parent.Text:lower() == "dash" then -- they could type "DASH", "dAsH", etc
        player.Character.Humanoid.WalkSpeed = 70
        script.Parent.Text = "Walk"
    elseif script.Parent.Text == "Walk" then
       player.Character.Humanoid.WalkSpeed = 25
       script.Parent.Text = "Dash" 
    end
end

script.Parent.MouseButton1Click:Connect(run)

On a side note, RBXScriptSignal:connect() is deprecated, use RBXScriptSignal:Connect() instead.

Answer this question