KodakKid3
|
  |
| Joined: 25 Dec 2008 |
| Total Posts: 2860 |
|
|
| 06 Apr 2015 07:35 PM |
This is a shift to sprint running script, it was a free model, I have a few questions on how it works.
1. Why does it need to be placed in the StarterGui rather than the Workspace? It has no GUIs or anything 2. What is the purpose of the first line, what does it mean? 3. What is the m variable, and why is it needed?
Thanks
--Place in the StarterGui. repeat wait() until game.Players.LocalPlayer
Spd = 32 --Run speed m = game.Players.LocalPlayer:GetMouse()
m.KeyDown:connect(function(key) if key == "0" then --"Shift to run" 0 == shift print("Running") game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = Spd end end)
m.KeyUp:connect(function(key) if key == "0" then game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16 end end) |
|
|
| Report Abuse |
|
|
|
| 06 Apr 2015 07:36 PM |
| This freemodel is bad. Needs to use UserInputService |
|
|
| Report Abuse |
|
|
KodakKid3
|
  |
| Joined: 25 Dec 2008 |
| Total Posts: 2860 |
|
|
| 06 Apr 2015 07:39 PM |
The reason I'm using this model is because it was only one script, and all other sprint models involved 2 scripts, so I figured this would be easier for me to understand.
It seems to work perfectly fine, why is it bad that it doesn't use UserInputService? And what is a UserInputService? |
|
|
| Report Abuse |
|
|
rayk999
|
  |
| Joined: 18 Feb 2011 |
| Total Posts: 4705 |
|
|
| 06 Apr 2015 07:53 PM |
1. Putting localscripts into StarterGui or StarterPack will give the players the localscripts when they spawn.
2. I don't see the point of the 1st line, you could just do
player = game.Players.LocalPlayer
3. m is the mouse object of the player. This script needed it to detect keydown and keyup events. Though, you should use UserInputService nowadays |
|
|
| Report Abuse |
|
|
KodakKid3
|
  |
| Joined: 25 Dec 2008 |
| Total Posts: 2860 |
|
|
| 06 Apr 2015 08:01 PM |
@ray I replaced your your line 1 with his and the script still works perfectly, but I don't understand the purpose of it. Whats the point of making a player variable if you don't use it at all in the script? Or is it not a variable?
Also, what is UserInputService and how would I use that instead? |
|
|
| Report Abuse |
|
|
amanda
|
  |
| Joined: 21 Nov 2006 |
| Total Posts: 5925 |
|
|
| 06 Apr 2015 08:09 PM |
local UIS = game:GetService('UserInputService')
local players = game:GetService('Players') local player = players.LocalPlayer local character = player.Character or player.CharacterAdded:wait() local humanoid = character.Humanoid or character:WaitForChild('Humanoid')
local defaultSpeed = 16 local sprintSpeed = defaultSpeed * 2
UIS.InputBegan:connect(function(inputObject, gameProcessedEvent) if not gameProcessedEvent then if inputObject.KeyCode == Enum.KeyCode.LeftShift then humanoid.WalkSpeed = sprintSpeed end end end)
UIS.InputEnded:connect(function(inputObject, gameProcessedEvent) if not gameProcessedEvent then if inputObject.KeyCode == Enum.KeyCode.LeftShift then humanoid.WalkSpeed = defaultSpeed end end end) |
|
|
| Report Abuse |
|
|
|
| 06 Apr 2015 08:10 PM |
because its a localscript
Save the plants! Plants > animals! |
|
|
| Report Abuse |
|
|
amanda
|
  |
| Joined: 21 Nov 2006 |
| Total Posts: 5925 |
|
|
| 06 Apr 2015 08:15 PM |
UserInputService is a modern tool for developers, it allows multi-platform input-detection. It can get keys on the keyboard, the mouse, as well as recognize different Touch screen inputs.
It has replaced KeyDown and KeyUp, it is more efficient and has a broader range of what it can do. It's a uniform way of detecting input. |
|
|
| Report Abuse |
|
|
KodakKid3
|
  |
| Joined: 25 Dec 2008 |
| Total Posts: 2860 |
|
|
| 06 Apr 2015 08:41 PM |
@amanda Questions about your script:
Couldn't you combine the first two variables just into this?: local player =game:GetService('Players').LocalPlayer
In your character and humanoid variables, why do you have the or parts?
And I understand the purpose of the two functions, but can you explain how they work?
I'm kind of new to a lot of this, thanks a lot. |
|
|
| Report Abuse |
|
|
vat21s
|
  |
| Joined: 07 Jun 2010 |
| Total Posts: 2508 |
|
|
| 06 Apr 2015 08:43 PM |
You could, variable = x or y
If x=nil then set it as y if y=nil then set it to nil. |
|
|
| Report Abuse |
|
|
rayk999
|
  |
| Joined: 18 Feb 2011 |
| Total Posts: 4705 |
|
|
| 06 Apr 2015 08:48 PM |
@Kodak
For my first answer, it works the same because the creator of that script was just being stupid. They had the script repeatedly wait until the player object existed. But, that localscript only runs in the player object so when it's created, LocalPlayer already exists. |
|
|
| Report Abuse |
|
|
rayk999
|
  |
| Joined: 18 Feb 2011 |
| Total Posts: 4705 |
|
|
| 06 Apr 2015 08:49 PM |
| Oh yeah, and it's just good habit to include the player variable just in case you ever need it. |
|
|
| Report Abuse |
|
|
amanda
|
  |
| Joined: 21 Nov 2006 |
| Total Posts: 5925 |
|
|
| 06 Apr 2015 09:31 PM |
I could technically type this all out in one line, and I very well might if my Return key was broken.
Variables are useful, they are placeholders that make your code look neat and easier to understand, as well as save you a lot of typing if you use them more than once.
I could of not defined anything, and then said:
game:GetService('UserInputService').InputBegan:connect(function(inputObject, gameProcessedEvent) if not gameProcessedEvent then if inputObject.KeyCode == Enum.KeyCode.LeftShift then game:GetService('Players').LocalPlayer.CharacterAdded:wait().Humanoid.WalkSpeed = 32 end end end)
For me, since I know what it all does, that doesn't look too bad, but I would still format it the way I did if I saw this, because it seems messy, and would only be OK if I was using it once and I was in a race to see how fast I could type it for functionality.
Which would be another circumstance I might write it all in one line. However, what if I want to get the player some other place in the script? Change a property? I didn't make a variable, I have to define it all over again.
-- local UIS = game:GetService('UserInputService')
local players = game:GetService('Players') local player = players.LocalPlayer local character = player.Character or player.CharacterAdded:wait() local humanoid = character.Humanoid or character:WaitForChild('Humanoid') --
All of the variables, just make sure everything I want to use is loaded, and easy to reference. A humanoid variable there is a bit unusual, however this specific code asked of the humanoid, so I went ahead and made my script that much nicer.
-- local defaultSpeed = 16 local sprintSpeed = defaultSpeed * 2 --
I keep these separate from the other variables, because these aren't constants, these are script specific variables that you will likely play with.
You could set sprintSpeed directly to a number, however I decided to take advantage of the pre-existing variable to make a rule.
-- UIS.InputBegan:connect(function(inputObject, gameProcessedEvent) --
InputBegan is an event that triggers whenever user input starts. Such as, it will be triggered when you press a key on your keyboard.
inputObject, in this case, your key being pressed
gameProcessedEvent, for all we are concerned, says whether or not the key being pressed is being typed into the chat, true if it is.
-- if not gameProcessedEvent then --
As described above, this checks to make sure someone isn't typing in the chat to trigger this, because we only want them to sprint when they aren't typing.
-- if inputObject.KeyCode == Enum.KeyCode.LeftShift then --
Something that's important to know about, is Enumeration, commonly called Enum. Now Enums, are whenever there is a set of options, in this case set forth by the ROBLOX Development team for our convenience.
One Enum property, is KeyCode. We get this Enum property through dot notation as follows:
-- Enum.KeyCode --
There is a list of KeyCodes that we can choose from, including nearly every single key on a standard keyboard.
http://wiki.roblox.com/index.php?title=API:Enum/KeyCode
It's like checking any property, except the answer isn't a string, or a number, it is an Enum.
Basically you can keep the format and just change the KeyCode at the end to whatever key you want to check for.
-- humanoid.WalkSpeed = sprintSpeed --
thanks to are friendly variables at the top, this was short and sweet and easy to read
any more questions? |
|
|
| Report Abuse |
|
|
KodakKid3
|
  |
| Joined: 25 Dec 2008 |
| Total Posts: 2860 |
|
|
| 07 Apr 2015 09:06 AM |
Thanks a ton, this makes sense now.
My only question is, how do you get to know all of these? I looked up InputBegan, InputObject, and gameProcessedEvent on the roblox wiki, and there are so many other properties, functions, and events of the UserInputService, and that's just one service.
Does it come purely from experience (By doing things like what I'm doing with this sprinting code)? Or did you specifically go on the wiki and try to learn all of them? |
|
|
| Report Abuse |
|
|
amanda
|
  |
| Joined: 21 Nov 2006 |
| Total Posts: 5925 |
|
|
| 07 Apr 2015 04:53 PM |
if you go to userinputservice on the wiki
like 90% of that is for touch devices
if all you want to do is mess with the keyboard and mouse, InputBegan, InputEnded, and InputChanged.
--
Imagine InputBegan and InputEnded as KeyDown and KeyUp(with an added bonus you can see if people are pressing or releasing a button on the mouse as well), and imagine InputChanged is what you want to use if you want to see if people are moving their mouse(with this you can also constantly get the exact 2D coordinates of the mouse on your screen in real time).
You can play with the Touch-screen functions, but they don't become anything significant unless you have already made a game have decided you now want to make it touch-screen compatible as well.
I use the wiki as a constant reference. |
|
|
| Report Abuse |
|
|