Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Can I call a function with an if statement?

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

✓ Accepted answer

@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)

2 comments

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?

0

Since I have a keydown script it has to be local but i also want the keydown script to play an animation.

Technically you could do it that way; for example:

if conditionhere = true then
script.Parent.Parent.Humanoid.Jump:connect(name)
end

4 comments

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.

0

Thank you for the help and sorry for downvoting.

1

This won't work. Look at his comments, he's trying to cross the Client<->Server boundary.

0

conditionhere = true should be conditionhere == true. A single = means assignment, double means equality test.

This discussion is preserved as it was. New answers, comments, and votes are closed.