Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Needing to know how either return or nil variables work in my situation?

Asked by 4 years ago

Premise: I have a sword. When I hold right click, a localscript will detect mouse.Button2Down and fire a remote event which will be caught by a function we can call function1 in a serverscript. in function1, i create a looped animation of me holding my arm in place, and when mouse.Button2Up I want to stop the animation.

Trouble is, I cannot access local variables from another function.

for a simple example, I already tried and failed to do this: local animation

function

animation = new instance of animation
animation:Play()

end

function

animation:Stop()

end

...Such that the variable is created outside both functions, then defined in the first one, and from there the second function can use the animation variable to fetch that animation i created in function1. But it didn't work.

I did some more research and someone told me I could use return instead. I looked everywhere for how to use return, and somehow the internet never fails to give me a practical example of return that's understandable to someone who hasnt been scripting for decades. in addition, the examples they gave at all were normal functions with no parameters, while my function1 did have parameters and player as an automatic parameter (because this function1 caught the remote event fired in the localscript far aforesaid)

Does anyone understand what I just said? In short, since nil variables aren't working for me, I was told I could use return, but I do not completely know the syntax, and the internet won't give me one. Can one of you present me with the solution?

0
There is a fault in the AnimationTrack onloading, you'd be raised an "attempt to index nil" error if there was an issue with the variable scope. Ziffixture 6913 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

Now as far as your problem is concerned, you actually don't need any of these solutions. Instead of connecting to a different function depending if 1Up or 1Down is pressed, connect to the same function, but send a small argument that modifies what you're trying to do. Here's a quick example of that:

Server:

--Server
local function animation(argument)
    if argument then
        --play animation
    else
        --stop animation
    end
end

RemoteEvent.OnServerEvent:Connect(animation)

Client:

--Client
--1Up
RemoteEvent:FireServer() --Sent nothing so play the animation

--1Down
RemoteEvent:FireServer(1) --Sent something so stop the animation

Of course you'll need a way to prevent the client from spamming the button, so most scripters do a check on the client first (normal player spammed button), and on the server (exploiters can work around your client check and spam it anyway)

For completeness, I'll also explain returns. Returns simply just return whatever value(s) you specify back to whatever called the function. Extra values returned that aren't set as a variable on the calling end will be completely ignored. If no return value is set, then the default value is nil. Here is a little cheat sheet I just made so you can see some of the different ways return can be used:

local function sample1(argument1, argument2) --argument2 is nil because we only passed one value
    print(argument1, argument2) --prints 0 nil
    return argument1 + 1 --returns 1 because argument1 is 0
end

local variable1, variable2 = sample1(0) --variable2 is nil since we only returned one value
print(variable1,variable2) -- prints 1 nil

And here are a few more examples of the functionality of return:

local num, num1 = 0, 5  --sets num as 0 and num1 as 5

local function sample1(argument1, argument2, argument3)
    if argument1 then -- if argument1 is not nil/false then
        return argument1 + 1
    elseif argument2 then
        return --This returns nil
    elseif argument3 == 5 then
        return sample1(argument3) --This will recall the same function and replace argument1 with argument3, then return that value
    end
    print("This will not print because return stops subsequent code from running")
end

local variable1 = sample1(1)
print(variable1) --prints 2
variable1 = sample1(false, 0) --avoiding the first if check
print(variable1) --prints nil because the elseif returned nil
variable1 = sample1(nil, nil, 5) --Accesses the argument3 check
print(variable1) -- prints 6
0
I had full intentions of using num and num1, but I guess I forgot lol. Let's just imagine the 0 is num and the 5 is num1 SteelMettle1 394 — 4y
Ad

Answer this question