|
So I'm making a sword that increases your walkspeed when you equip, then resets it back to what it was before when you unequip
the thing is that my game include some speed-enhancing objects, so I don't want to reduce it straight back to 16, or that could become a problem. I want the script to get the player's current walkspeed when they equips the sword, assign it as a variable or something somewhere, and then call back to it when the need arises (when it's unequipped), but I was struggling to do that
Can any of you could tell me how I could add that into my script?
Thanks in advance |
|
|
|
Well, if multiple scripts are using the same variable, and they are only on one side (server/local preferably only used in local for beginners), then why not just use _G?
http://wiki.roblox.com/index.php?title=Global_function
I mean, there is more efficient ways to do this, but they require more lines of code, with globals you can literally just set by "_G.walkspeed = " and get by "_G.walkspeed" in the same script or any other that's in the same side. |
|
|
Sorry, quite new to this. Wiki doesn't explain what a _G is, mind offering a small explanation?
Thanks! |
|
|
local equipped=false
Tool.Equipped:connect(function(mouse)
equipped=true
end)
Tool.Unequipped:connect(function(mouse)
equipped=false
end)
if equipped=true then
blah blah walkspeed blah blah = your walkspeed
else
walkspeed blah blah = your unequipped walkspeed
end
y-y-you too... |
|
|
if equipped==true*
sorry. lol
y-y-you too... |
|
|
@electro
Please read the post! |
|
|
blah, sorry... I should really read through before posting some irrelevant crap.
y-y-you too... |
|
llaserxJoin Date: 2011-12-10 Post Count: 53069 |
function get()
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:wait()
local h = char.Humanoid.WalkSpeed
return h
end
if get() then
print ("" ..get())
end
http://www.roblox.com/xla-item?id=290739801http://www.roblox.com/aser-item?id=290739819http://www.roblox.com/23-item?id=290739831 R$2,881 Tx343 (づ ゚ ³ ゚)づ |
|
|
_G is a table in Lua that stores all the global variables. If you store a variable there, you can access it in other scripts.
like this:
-- script 1
_G.foo = "bar"
-- script 2
print(_G.foo)
--the output is bar
https://www.youtube.com/watch?v=onhFH7jpq2c |
|
|
@xlaser Thank you so much! |
|
|
@cp
Thanks for the explanation!
_G is irrelevant in this case though, I just wanted to get a char's walkspeed, it's good now though, thanks guys! |
|
|
local Player = game:GetService("Players").LocalPlayer -- I don't know exactly what you're doing, but this would be the 'Player' Instance we shall need :^)
local WalkSpeed -- Don't exactly want to set this in the if statement, or the rest of the script can't access it :U
if Player.Character and Player.Character:FindFirstChild("Humanoid") then --Checking if the Player has a Character, and a Humanoid
WalkSpeed = Player.Character.Humanoid.WalkSpeed -- http://wiki.roblox.com/index.php?title=API:Class/Humanoid/WalkSpeed
end
-- That was just a example, after that if statement it would have the current Player's WalkSpeed stored in the value "WalkSpeed", but you don't necessarily need a value.
local Player = game:GetService("Players").LocalPlayer
-- For this example, I wont have the if statement to check if the player has a Character.
Player.Character.Humanoid.WalkSpeed = Player.Character.Humanoid.WalkSpeed + 10 -- This will give the Player 10 more WalkSpeed, so if they had 16 before now they have 26. You could use subtraction as well to make them slower.
I hope this was more helpful! |
|