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.