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

LocalScript works in Studio but not in Player Mode. What can be done to make it work?

Asked by
RAYAN1565 691 Moderation Voter
5 years ago
Edited 5 years ago

Update: I have figured out what seems to be the problem, although I don't know how I should go about fixing it. It appears that when referencing back to humanoid, the script results in an error in player mode.

SOLUTION: Replacing the first line with local humanoid = workspace:WaitForChild(game.Players.LocalPlayer.Name):WaitForChild("Humanoid") did the trick. If you have a better or more effective solution, feel free to answer.

I wrote a script that alters a characters' walkspeed when left shift is pressed and jumping power when the space button is pressed. All of the information regarding ... Stamina.Value works as desired. The problem is that this script works in studio but not in player mode. For instance, when I press the respective keys in studio, the script alters the walkspeed and jumping power. However, this is not the case in player mode. In player mode, the walkspeed and jumping power does not change.

This script is a local script placed within another local script that is placed within StarterGui. The layout looks like this:

  • StarterGui

    • ScreenGUI

      • Frame

        • Local Script 1

          • IntValue, titled "Stamina"

          • Local Script 2 (the script I am referring to)

I don't understand what the issue is because I have a similar script to this one, except it alters the stamina value of an IntValue and it works as desired in both studio and player mode. Yet, this script does not want to make my life easy. See below:

--Indexing variables [This works].
local humanoid = game.Players.LocalPlayer.Character:FindFirstChild("Humanoid")
local input = game:GetService("UserInputService")
local leftShift, space = 0, 0
local t, f = true, false

--Using InputBegan and InputEnd to figure out whether the keys are pressed or not. [This works].
input.InputBegan:Connect(function(key)
    if (key.KeyCode == Enum.KeyCode.LeftShift) then
        leftShift = t
    elseif (key.KeyCode == Enum.KeyCode.Space) then
        space = t
    end
end)

input.InputEnded:Connect(function(key)
    if (key.KeyCode == Enum.KeyCode.LeftShift) then
        leftShift = f
    elseif (key.KeyCode == Enum.KeyCode.Space) then
        space = f
    end
end)

--Running a function that executes code depending on whether a key is pressed or not. [Does not work].
function walkSpeedProc() -- Executes well in studio but not in player mode. 
    if leftShift == t and script.Parent.Stamina.Value < 6 then
        humanoid.WalkSpeed = 16
        humanoid.JumpPower = 0
    elseif leftShift == t and script.Parent.Stamina.Value >= 6 then
        humanoid.WalkSpeed = 25
    elseif leftShift == f then
        humanoid.WalkSpeed = 16
    end

    if space == t and script.Parent.Stamina.Value >= 7 then
        humanoid.JumpPower = 50
    elseif space ==  t and script.Parent.Stamina.Value < 7 then
        humanoid.JumpPower = 0
    elseif space == f then
        humanoid.JumpPower = 0
    end
end

--Loops the function written previously to check repeatedly for key presses and what not. [Does not work].
while true do
    wait()
    walkSpeedProc()
end

If more information is needed, please let me know and I will provide you with the extra information.

0
In Studio's Play Solo mode, there is no separation of the server and client. User#19524 175 — 5y
1
Can you explain further? RAYAN1565 691 — 5y
0
I'm going to play a hunch and say that maybe using ":WaitForChild()" to index 'humanoid' would solve this problem. RAYAN1565 691 — 5y
0
Why add a loop and a function instead of putting the code that changes the walkspeed and jumppower in the input checking part of the script. poke7667 142 — 5y
View all comments (3 more)
0
Also, the character might not be added in yet so use WaitForChild to index Character or do this: local Character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait() poke7667 142 — 5y
1
^ That wouldn't work. If you spammed the key buttons, the code will execute many times (debounce wouldn't work in a case like that). I have solved my own problem by using ":WaitForChild("Humanoid")". Thanks though! RAYAN1565 691 — 5y
0
No, he means you should not check for the character in Workspace, which is correct. A player named Terrain or Part could join the game messing up some scripts. Use the Character property and that only to get the character. User#19524 175 — 5y

Answer this question