generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Why does this stamina script break when the player dies?

Previous Thread :: Next Thread 
fishguy100 is not online. fishguy100
Joined: 16 Feb 2013
Total Posts: 3679
12 Mar 2016 11:15 AM
--\\ Services
local Players = game:GetService("Players");
local UserInputService = game:GetService("UserInputService");

--\\ LocalPlayer Variables
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local Humanoid = Character.Humanoid
local StaminaValue = Player:WaitForChild("Stamina")

--\\ Debounce Variables
local Running = false

--\\ Getting Input
UserInputService.InputBegan:connect(function(Input,GPE)
if Input.UserInputType == Enum.UserInputType.Keyboard and not GPE and not Running then
if Input.KeyCode == Enum.KeyCode.LeftShift then
Running = true
end
end
end)

UserInputService.InputEnded:connect(function(Input,GPE)
if Input.UserInputType == Enum.UserInputType.Keyboard and not GPE and Running then
if Input.KeyCode == Enum.KeyCode.LeftShift then
Running = false
end
end
end)

game:GetService("RunService").RenderStepped:connect(function()
if Running and StaminaValue.Value > 0 then
Humanoid.WalkSpeed = 23
StaminaValue.Value = StaminaValue.Value > 0 and StaminaValue.Value - 1 or StaminaValue.Value
if StaminaValue.Value < 1 then
Humanoid.WalkSpeed = 16
Running = false
end
else
Humanoid.WalkSpeed = 16
StaminaValue.Value = StaminaValue.Value < 100 and StaminaValue.Value + 1 or StaminaValue.Value
end
end)

There are no errors. It works once but then breaks when the player dies. Help please
Report Abuse
fishguy100 is not online. fishguy100
Joined: 16 Feb 2013
Total Posts: 3679
12 Mar 2016 11:44 AM
bump
Report Abuse
KLGA is not online. KLGA
Joined: 19 Apr 2014
Total Posts: 2571
12 Mar 2016 11:49 AM
Might be because of this:

game:GetService("RunService").RenderStepped:connect(function()
if Running and StaminaValue.Value > 0 then
Humanoid.WalkSpeed = 23
StaminaValue.Value = StaminaValue.Value > 0 and StaminaValue.Value - 1 or StaminaValue.Value
if StaminaValue.Value < 1 then
Humanoid.WalkSpeed = 16
Running = false
end
else
Humanoid.WalkSpeed = 16
StaminaValue.Value = StaminaValue.Value < 100 and StaminaValue.Value + 1 or StaminaValue.Value
end
end)

More specifically, this:

Humanoid.WalkSpeed = 23

That runs with every single frame of the game, when the player respawns there is a short period of time where their Character is nil. Try changing how you define Humanoid to something like:

local Humanoid = Character:WaitForChild("Humanoid")
Report Abuse
fishguy100 is not online. fishguy100
Joined: 16 Feb 2013
Total Posts: 3679
12 Mar 2016 11:51 AM
I just did that and it still didn't work :(
Report Abuse
KLGA is not online. KLGA
Joined: 19 Apr 2014
Total Posts: 2571
12 Mar 2016 11:53 AM
Make sure 'running' is true and 'StaminaValue' is > 0.

Print them out at the beginning and see what their values are:

game:GetService("RunService").RenderStepped:connect(function()
print(Running,StaminaValue.Value)
if Running and StaminaValue.Value > 0 then
Humanoid.WalkSpeed = 23
StaminaValue.Value = StaminaValue.Value > 0 and StaminaValue.Value - 1 or StaminaValue.Value
if StaminaValue.Value < 1 then
Humanoid.WalkSpeed = 16
Running = false
end
else
Humanoid.WalkSpeed = 16
StaminaValue.Value = StaminaValue.Value < 100 and StaminaValue.Value + 1 or StaminaValue.Value
end
end)
Report Abuse
fishguy100 is not online. fishguy100
Joined: 16 Feb 2013
Total Posts: 3679
12 Mar 2016 11:55 AM
When I reset and I pressed shift, I started jumping

But what printed was "false 100"
Report Abuse
KLGA is not online. KLGA
Joined: 19 Apr 2014
Total Posts: 2571
12 Mar 2016 11:58 AM
I tested your script and it seemed to work, Running was changing to false and true depending on whether or not shift was pressed.
Report Abuse
fishguy100 is not online. fishguy100
Joined: 16 Feb 2013
Total Posts: 3679
12 Mar 2016 12:01 PM
It doesn't work for me ;-;

Want to see?
Report Abuse
KLGA is not online. KLGA
Joined: 19 Apr 2014
Total Posts: 2571
12 Mar 2016 12:02 PM
When you print the Running value, it's not changing at all when you press shift?
Report Abuse
fishguy100 is not online. fishguy100
Joined: 16 Feb 2013
Total Posts: 3679
12 Mar 2016 12:04 PM
no. Follow me into my place called "Quake Mayhem"
Report Abuse
ray_revenge is not online. ray_revenge
Joined: 18 Feb 2011
Total Posts: 453
12 Mar 2016 12:07 PM
don't change the speed locally


Recommended username: MarkedRay_revenge


Report Abuse
KLGA is not online. KLGA
Joined: 19 Apr 2014
Total Posts: 2571
12 Mar 2016 12:09 PM
I don't really think there would be a difference^
Report Abuse
ray_revenge is not online. ray_revenge
Joined: 18 Feb 2011
Total Posts: 453
12 Mar 2016 12:10 PM
I tested it in studio
i assume roblox did it for security reasons

make a localscript set the localplayer's speed to 40 or w/e, reset, then it won't work


Recommended username: JumbledRay_revenge


Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image