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 » Scripting Helpers
Home Search
 

Hi, need help with event triggers

Previous Thread :: Next Thread 
ColIide is not online. ColIide
Joined: 21 Nov 2008
Total Posts: 154
25 Jul 2014 07:12 AM
I'm trying to make a script that when the shift key is pressed your character sprints but I'm having trouble with the events to trigger this. My code looks like this:


local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local regen = true

function setup()
player.Humanoid.Walkspeed = 12
stam = Instance.new("NumberValue", player)
stam.Value = 100
stam.Name = stam
maxstam = Instance.new("NumberValue", player)
maxstam.Value = 100
maxstam.Name = maxstam
stamregen()
end

function sprint(key)
if key == 48 and stam.Value > 0 then --left shift
regen = false
player.Humanoid.Walkspeed = 18
while stam.Value > 0 do
stam.Value = stam.Value - 1
wait(0.05)
end
player.Humanoid.Walkspeed = 12
wait(2)
regen = true
end
end

function roll(key)
if key == "c" and stam.Value > 25 then
regen = false
--roll
stam.Value = stam.Value - 25
wait(2)
regen = true
end
end

function stamregen()
while player == true do
while maxstam > stam and regen == true do
stam.Value = stam.Value + 1
wait(0.05)
end
wait(0.05)
end
end

mouse.KeyDown:connect(sprint)

game.Players.PlayerAdded:connect(function(player)
player.CharacterAdded:connect(setup())
end)

ANY help is greatly appreciated!!
Report Abuse
Immacularity is not online. Immacularity
Joined: 21 Jun 2014
Total Posts: 419
25 Jul 2014 07:31 AM
"if key == 48"
"key" is always a string.
Change it to if key == "0"
Should work fine.
Report Abuse
ColIide is not online. ColIide
Joined: 21 Nov 2008
Total Posts: 154
25 Jul 2014 08:09 AM
Thanks I changed the value to a string value however my script does not appear to running the setup function which should trigger when a character is added/respawned. Any idea about this?
Report Abuse
AnonyAnonymous is not online. AnonyAnonymous
Joined: 23 Jun 2013
Total Posts: 6332
25 Jul 2014 08:24 AM
Try switching the last three lines with,
game.Players.PlayerAdded:connect(function(player)
player.CharacterAdded:connect(setup)
end)
Report Abuse
ColIide is not online. ColIide
Joined: 21 Nov 2008
Total Posts: 154
25 Jul 2014 08:29 AM
I made the change as I think you're right and am keeping it like so but it didn't seem to make any effect. Possible because I'm running it in the studio edit test environment and it loads it in weirdly. I'm not sure.
Report Abuse
AnonyAnonymous is not online. AnonyAnonymous
Joined: 23 Jun 2013
Total Posts: 6332
25 Jul 2014 08:29 AM
Are you receiving any errors via output?.
Report Abuse
lama321 is not online. lama321
Joined: 18 Dec 2011
Total Posts: 300
25 Jul 2014 08:33 AM
Also, make sure your testing it in a server (go to test, then start server with 1 player). I dont think PlayerAdded is fired when you just test normally (although i could be wrong, havent used roblox in a while)
Report Abuse
lama321 is not online. lama321
Joined: 18 Dec 2011
Total Posts: 300
25 Jul 2014 08:37 AM
oh, and WalkSpeed is a property of humanoid object (in the character) not the player
Report Abuse
ColIide is not online. ColIide
Joined: 21 Nov 2008
Total Posts: 154
25 Jul 2014 08:38 AM
I'm not receiving any error messages about syntax or anything and I'll start testing with a server from now on. The server tests don't work either atm
Report Abuse
lama321 is not online. lama321
Joined: 18 Dec 2011
Total Posts: 300
25 Jul 2014 08:39 AM
oh, and it might be a good idea to change

game.Players.PlayerAdded:connect(function(plr)
plr.CharacterAdded:connect(setup())
end)

as you have already declared player above
Report Abuse
AnonyAnonymous is not online. AnonyAnonymous
Joined: 23 Jun 2013
Total Posts: 6332
25 Jul 2014 08:39 AM
Where is the LocalScript located?.
Report Abuse
ColIide is not online. ColIide
Joined: 21 Nov 2008
Total Posts: 154
25 Jul 2014 08:41 AM
I added in my variables:
local character = player.character
And then edited player.Humanoid.Walkspeed = 12 to character.Humanoid.Walkspeed to fix that issue. However it still doesn't set the walkspeed to 12.
Report Abuse
ColIide is not online. ColIide
Joined: 21 Nov 2008
Total Posts: 154
25 Jul 2014 08:42 AM
The localscript is in workspace
Report Abuse
lama321 is not online. lama321
Joined: 18 Dec 2011
Total Posts: 300
25 Jul 2014 08:42 AM
Move the localscript to starter pack, localscripts only work in the backpack
Report Abuse
lama321 is not online. lama321
Joined: 18 Dec 2011
Total Posts: 300
25 Jul 2014 08:43 AM
or playergui, or character
Report Abuse
AnonyAnonymous is not online. AnonyAnonymous
Joined: 23 Jun 2013
Total Posts: 6332
25 Jul 2014 08:44 AM
Actually, it would work just as well in the StarterGui.
Report Abuse
ColIide is not online. ColIide
Joined: 21 Nov 2008
Total Posts: 154
25 Jul 2014 08:44 AM
Aaah this is the main problem thanks!

Now I have the error that character is not a valid member of Player however I think I'll be able to try fix bugs on my own. Any help you want to give is not necessary right this moment but is still helpful.

Thanks all!
Report Abuse
lama321 is not online. lama321
Joined: 18 Dec 2011
Total Posts: 300
25 Jul 2014 08:46 AM
By the way, you need to replace player.character with player.Character
Report Abuse
AnonyAnonymous is not online. AnonyAnonymous
Joined: 23 Jun 2013
Total Posts: 6332
25 Jul 2014 08:46 AM
Make sure that "Character" is capitalized correctly.
Report Abuse
ColIide is not online. ColIide
Joined: 21 Nov 2008
Total Posts: 154
25 Jul 2014 08:54 AM
I'm currently getting the error Players.Player1.Backpack.LocalScript:21: attempt to index global 'stam' (a nil value)

This is due to the setup function not being executed and hence creating the NumberValue named stam.

Is something obviously wrong with this:

local player = game.Players.LocalPlayer
local character = player.Character
local mouse = player:GetMouse()
local regen = true

function setup()
if character == true then
character.Humanoid.Walkspeed = 12
stam = Instance.new("NumberValue", character)
stam.Value = 100
stam.Name = stam
maxstam = Instance.new("NumberValue", character)
maxstam.Value = 100
maxstam.Name = maxstam
stamregen()
end
end

game.Players.PlayerAdded:connect(function(plr)
plr.CharacterAdded:connect(setup)
end)
Report Abuse
ColIide is not online. ColIide
Joined: 21 Nov 2008
Total Posts: 154
25 Jul 2014 09:32 AM
Bump
Report Abuse
ColIide is not online. ColIide
Joined: 21 Nov 2008
Total Posts: 154
25 Jul 2014 09:56 AM
I've solved all previous issues but I can't seem to get my sprint to work for when the shift key is pressed down.

Currently I'm using:

function sprint(key)
if key == "0" and stam.Value > 0 then --left shift
regen = false
character.Humanoid.WalkSpeed = 18
while stam.Value > 0 and key == "0" do --THIS LINE NEEDS FIXING
stam.Value = stam.Value - 1
wait(0.05)
end
character.Humanoid.WalkSpeed = 12
wait(2)
regen = true
end
end

With this function I haven't been able to get it to recognize its no longer being pressed yet for use in the while loop.
Report Abuse
Waffloid is online. Waffloid
Joined: 14 Jul 2011
Total Posts: 1606
25 Jul 2014 10:02 AM
Make it that once the key is Shift make it turn the var sh true and then make it if sh is false then to break. Also make a KeyUp function that makes sh false.

~Swagilitious and not Nutricious~
Report Abuse
ColIide is not online. ColIide
Joined: 21 Nov 2008
Total Posts: 154
25 Jul 2014 10:07 AM
Thanks all sorted! Great :D
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • 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