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()
@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)
Technically you could do it that way; for example:
if conditionhere = true then script.Parent.Parent.Humanoid.Jump:connect(name) end