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

Can I call a function with an if statement?

Asked by
yoshiegg6 176
9 years ago

For example if I wanted to have a local script call a function in a server script could i do this?

--Server script code
function name()
A = Instance.new("Animation")
A.AnimationID = "ID here"
Track = char.Humanoid:LoadAnimation(A)
wait(1)
track:Play()

--local script code
if script.Parent.Parent.Humanoid.Jump == true then
name()

2 answers

Log in to vote
2
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

@OP; No, you can't. (Also, you're missing end statements.)

Scripts have a thing called "scope". Scope is every variable that the line being run has access to. local is used, for example, to declare scope within a code block:

local x = 5

if x == 5 then
    print(x) --> 5
    local y = "7"
    print(y) --> 7
end

print(x) --> 5
print(y) --> 7

--Because 'y' was declared locally, *within* the `if` block, it is removed from the scope when the `if` block ends.

Going a step higher, each individual Script has its own Scope. This includes LocalScripts.

The normal way to get around this is to use a global Table: _G or shared, however each Client (including the Server) gets its own copy of this Table. They are not replicated.

The only way to cross the Server<->Client boundary is through the Value objects (which don't work with FilteringEnabled) and the Remote* objects.

If you want a yielding function call, use a RemoteFunction (Yielding means that the LocalScript will wait for the invoked function to return before resuming.):

--Server script code
--(I have made this slightly more efficient, compared to your code.)
local A = Instance.new("Animation", char)
A.AnimationID = "ID here"

local RemoteFunc = Instance.new("RemoteFunction")
RemoteFunc.Name = "RemoteFunction"
RemoteFunc.Parent = game.ReplicatedStorage

function RemoteFunc.OnServerInvoke(player)
    Track = player.Character.Humanoid:LoadAnimation(A)
    wait(1)
    Track:Play() --Capitalization matters! In Lua, track ~= Track!
end
--THIS SHOULD ONLY RUN ONCE: do not put it inside of a PlayerAdded function.

--local script code
local RemoteFunc = game.ReplicatedStorage:WaitForChild("RemoteFunction")

game.Players.LocalPlayer.Character.Humanoid.Jumping:connect(function()
    RemoteFunc:InvokeServer() --You have to connect this event so that it runs every time. Unless you had other code that ran this repeatedly.
end)

If you want a fire-and-forget function call, use a RemoteEvent:

--Server script code
local A = Instance.new("Animation", char)
A.AnimationID = "ID here"

local RemoteEv = Instance.new("RemoteEvent")
RemoteEvName = "RemoteEvent"
RemoteEv.Parent = game.ReplicatedStorage

RemoteEv.OnServerEvent:connect(function(player)
    Track = player.Character.Humanoid:LoadAnimation(A)
    wait(1)
    Track:Play() --Capitalization matters! In Lua, track ~= Track!
end)
--THIS SHOULD ONLY RUN ONCE: do not put it inside of a PlayerAdded function.

--local script code
local RemoteEv = game.ReplicatedStorage:WaitForChild("RemoteEvent")

game.Players.LocalPlayer.Character.Humanoid.Jumping:connect(function()
    RemoteEv:FireServer()
end)
0
I'm asking because I hear if you play an animation in a local script it will only show for the local player, any idea how to get around that? yoshiegg6 176 — 9y
0
Since I have a keydown script it has to be local but i also want the keydown script to play an animation. yoshiegg6 176 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

Technically you could do it that way; for example:

if conditionhere = true then
script.Parent.Parent.Humanoid.Jump:connect(name)
end
1
Hmm. And maybe instead of downvoting my previous answer, you could ask me to revise it for you. See? I revised it for you with a new answer. It really irritates me that you would do that when I am trying to help. dirty_catheter 7 — 9y
0
Thank you for the help and sorry for downvoting. yoshiegg6 176 — 9y
1
This won't work. Look at his comments, he's trying to cross the Client<->Server boundary. adark 5487 — 9y
0
`conditionhere = true` should be `conditionhere == true`. A single `=` means assignment, double means equality test. noliCAIKS 210 — 9y

Answer this question