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
 

Re: Script can't keep up with players actions...

Previous Thread :: Next Thread 
Bananacb is not online. Bananacb
Joined: 13 Jun 2008
Total Posts: 5188
01 May 2016 11:49 PM
So I got this script here and it requires the use of W, A, S, and D keys on the keyboard to activate the script for the stamina GUI. The issue is if a player for example moves from W to A really quick without stopping then the script sees that you deactivated the W key, but then it doesn't catch onto the fact you already activated the A key. This is an issue cause the stamina bar goes up even though the player is moving (which is not what I want to happen.)
------------------------------------------------------------------------------------

local p = game.Players.LocalPlayer
local c = p.Character
local m = p:GetMouse()

local sprint = false

local t,f = true,false

local w,a,s,d = f,f,f,f
local wt = 0

m.KeyDown:connect(function(key)
if key == 'w' then
w = t
sprint = t
end
if key == 'a' then
a = t
sprint = t
end
if key == 's' then
s = t
sprint = t
end
if key == 'd' then
d = t
sprint = t
end
end)

m.KeyUp:connect(function(key)
if key == 'w' then
w = f
sprint = f
end
if key == 'a' then
a = f
sprint = f
end
if key == 's' then
s = f
sprint = f
end
if key == 'd' then
d = f
sprint = f
end
end)

while wait() do
wt = wt - .01
if wt <= 0 then
if script.Parent.Frame.Bar.Size.X.Scale < 1 then
script.Parent.Frame.Bar.Size = script.Parent.Frame.Bar.Size + UDim2.new(0.01,0,0,0)
end
end
if w or a or s or d then
if script.Parent.Frame.Bar.Size.X.Scale > 0 and sprint then
script.Parent.Frame.Bar.Size = script.Parent.Frame.Bar.Size - UDim2.new(0.001,0,0,0)
c.Humanoid.WalkSpeed = 20
end
end

if sprint then
wt = 1.3
end

if not w and not a and not s and not d then
c.Humanoid.WalkSpeed = 12
end

if not sprint then c.Humanoid.WalkSpeed = 12
end

if script.Parent.Frame.Bar.Size.X.Scale <= 0 then
c.Humanoid.WalkSpeed = 12
end
end
Report Abuse
Jammer622 is not online. Jammer622
Joined: 19 Nov 2008
Total Posts: 1739
02 May 2016 12:06 AM
You should update to using http://wiki.roblox.com/index.php?title=API:Class/UserInputService
There's a function called http://wiki.roblox.com/index.php?title=API:Class/UserInputService/GetKeysPressed that returns a table of all the keys currently down.
You could effectively trim the script down to less than half of its original length.
Report Abuse
Jammer622 is not online. Jammer622
Joined: 19 Nov 2008
Total Posts: 1739
02 May 2016 12:10 AM
If you want to keep with what you know, your original script's problem lays here:

m.KeyUp:connect(function(key)
if key == 'w' then
w = f
sprint = f
end
if key == 'a' then
a = f
sprint = f
end
if key == 's' then
s = f
sprint = f
end
if key == 'd' then
d = f
sprint = f
end
end)

For each key that is released, you turn sprinting back to false. That means if W is held, then A is held, then W is released, sprinting is turned from True to True to False, while A is still held.
To fix it, you'd just need to remove the individual sprint=f blocks and copy some code from your loop:

m.KeyUp:connect(function(key)
if key == 'w' then
w = f
end
if key == 'a' then
a = f
end
if key == 's' then
s = f
end
if key == 'd' then
d = f
end
if w or a or s or d then
sprint = f
end
end)
Report Abuse
Jammer622 is not online. Jammer622
Joined: 19 Nov 2008
Total Posts: 1739
02 May 2016 12:17 AM
Also, just by learning to use tables [http://wiki.roblox.com/index.php?title=Table] and slimming redundancies in your script, you can make it as simple as this, which.. should work?:

local p = game.Players.LocalPlayer
local c = p.Character
local m = p:GetMouse()

local sprint = false

local t,f = true,false

local keys = {}
local wt = 0

m.KeyDown:connect(function(key)
keys[key] = t
end)

m.KeyUp:connect(function(key)
keys[key] = t
end)

while wait() do
wt = wt - .01
if wt <= 0 then
if script.Parent.Frame.Bar.Size.X.Scale < 1 then
script.Parent.Frame.Bar.Size = script.Parent.Frame.Bar.Size + UDim2.new(0.01,0,0,0)
end
end
if (keys[w] or keys[a] or keys[s] or keys[d]) and script.Parent.Frame.Bar.Size.X.Scale > 0 then
if script.Parent.Frame.Bar.Size.X.Scale > 0 and sprint then
script.Parent.Frame.Bar.Size = script.Parent.Frame.Bar.Size - UDim2.new(0.001,0,0,0)
c.Humanoid.WalkSpeed = 20
end
wt = 1.3
else
c.Humanoid.WalkSpeed = 12
end
end
Report Abuse
Bananacb is not online. Bananacb
Joined: 13 Jun 2008
Total Posts: 5188
02 May 2016 12:33 AM
Wow I didn't know there was anyway to fix this, thanks so much! One issue though...on your last script above the script didn't work it said w, a, s, and d are all unknown globals.
Report Abuse
Jammer622 is not online. Jammer622
Joined: 19 Nov 2008
Total Posts: 1739
02 May 2016 12:35 AM
Might have been because I didn't define them in this new line.

local keys = {w=f,a=f,s=f,d=f}

Who knows?
Report Abuse
Bananacb is not online. Bananacb
Joined: 13 Jun 2008
Total Posts: 5188
02 May 2016 12:39 AM
Nope that didn't work hmmmmmmmmmm....
------------------------------------------------------------------
local p = game.Players.LocalPlayer
local c = p.Character
local m = p:GetMouse()

local sprint = false

local t,f = true,false

local keys = {w=f,a=f,s=f,d=f}
local wt = 0

m.KeyDown:connect(function(key)
keys[key] = t
end)

m.KeyUp:connect(function(key)
keys[key] = t
end)

while wait() do
wt = wt - .01
if wt <= 0 then
if script.Parent.Frame.Bar.Size.X.Scale < 1 then
script.Parent.Frame.Bar.Size = script.Parent.Frame.Bar.Size + UDim2.new(0.01,0,0,0)
end
end

if (keys[w] or keys[a] or keys[s] or keys[d]) and script.Parent.Frame.Bar.Size.X.Scale > 0 then
if script.Parent.Frame.Bar.Size.X.Scale > 0 and sprint then
script.Parent.Frame.Bar.Size = script.Parent.Frame.Bar.Size - UDim2.new(0.001,0,0,0)
c.Humanoid.WalkSpeed = 20
end
wt = 1.3
else
c.Humanoid.WalkSpeed = 12
end
end
Report Abuse
Jammer622 is not online. Jammer622
Joined: 19 Nov 2008
Total Posts: 1739
02 May 2016 01:05 AM
wow i did a dumb

it's supposed to be keys.@ not keys[@]. that assumes that, for example, keys[a] is linking to keys[value of variable "a"]. it should be

if (keys.w or keys.a or keys.s or keys.d) ...

you didnt even need to add anything to the keys = {} table
Report Abuse
Bananacb is not online. Bananacb
Joined: 13 Jun 2008
Total Posts: 5188
02 May 2016 11:50 AM
Interesting that didn't work either :/
-----------------------------------------------------------
local p = game.Players.LocalPlayer
local c = p.Character
local m = p:GetMouse()

local sprint = false

local t,f = true,false

local keys = {}
local wt = 0

m.KeyDown:connect(function(key)
keys[key] = t
end)

m.KeyUp:connect(function(key)
keys[key] = t
end)

while wait() do
wt = wt - .01
if wt <= 0 then
if script.Parent.Frame.Bar.Size.X.Scale < 1 then
script.Parent.Frame.Bar.Size = script.Parent.Frame.Bar.Size + UDim2.new(0.01,0,0,0)
end
end

if (keys.w or keys.a or keys.s or keys.d) and script.Parent.Frame.Bar.Size.X.Scale > 0 then
if script.Parent.Frame.Bar.Size.X.Scale > 0 and sprint then
script.Parent.Frame.Bar.Size = script.Parent.Frame.Bar.Size - UDim2.new(0.001,0,0,0)
c.Humanoid.WalkSpeed = 20
end
wt = 1.3
else
c.Humanoid.WalkSpeed = 12
end
end
Report Abuse
Jammer622 is not online. Jammer622
Joined: 19 Nov 2008
Total Posts: 1739
02 May 2016 11:22 PM
I must have forgotten to completely take out the sprint variable.
It's what I get for trying to answer every forum question as late as five in the morning.
This should be the last fix. I studio tested it again. If it's still broken, I'll rewrite the entire thing from scratch.

local p = game.Players.LocalPlayer
local c = p.Character
local m = p:GetMouse()

local t,f = true,false

local keys = {}
local wt = 0

m.KeyDown:connect(function(key)
keys[key] = t
end)

m.KeyUp:connect(function(key)
keys[key] = t
end)

while wait() do
wt = wt - .01
if wt <= 0 then
if script.Parent.Frame.Bar.Size.X.Scale < 1 then
script.Parent.Frame.Bar.Size = script.Parent.Frame.Bar.Size + UDim2.new(0.01,0,0,0)
end
end

if keys.w or keys.a or keys.s or keys.d then
if script.Parent.Frame.Bar.Size.X.Scale > 0 then
script.Parent.Frame.Bar.Size = script.Parent.Frame.Bar.Size - UDim2.new(0.001,0,0,0)
c.Humanoid.WalkSpeed = 20
end
wt = 1.3
else
c.Humanoid.WalkSpeed = 12
end
end
Report Abuse
Bananacb is not online. Bananacb
Joined: 13 Jun 2008
Total Posts: 5188
03 May 2016 04:28 AM
Well if you want any good news...it sorta works xD

When you press W, A, S and/or D the stamina goes down. However, the stamina doesn't go back up. Also when you pause the stamina doesn't go up.
Report Abuse
Jammer622 is not online. Jammer622
Joined: 19 Nov 2008
Total Posts: 1739
03 May 2016 11:23 PM
I told you if it didn't work the way you wanted it to, I'd rewrite it from scratch..

Player = game.Players.LocalPlayer
Human = Player.Character:WaitForChild("Humanoid")
Mouse = Player:GetMouse()
Ui, Stam, Cool = script.Parent.Frame.Bar, 1000, 130
MaxStam, MaxCool = Stam, Cool
Keys = {}
Mouse.KeyDown:connect(function(Key)
Keys[Key:upper()] = true
end)
Mouse.KeyUp:connect(function(Key)
Keys[Key:upper()] = false
end)
while wait() do
local Moving = Keys.W or Keys.A or Keys.S or Keys.D
Cool = Moving and MaxCool or math.max(Cool - 1, 0)
Stam = Moving and math.max(Stam - 5, 0) or (Cool == 0) and math.min(Stam + 10, MaxStam) or Stam
Human.WalkSpeed = (Stam > 0) and 20 or 12
Ui.Size = UDim2.new(Stam / MaxStam, 0, 1, 0)
end
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