✓ 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)
local y = "7"
print(y)
end
print(x)
print(y)
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.):
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()
end
local RemoteFunc = game.ReplicatedStorage:WaitForChild("RemoteFunction")
game.Players.LocalPlayer.Character.Humanoid.Jumping:connect(function()
RemoteFunc:InvokeServer()
end)
If you want a fire-and-forget function call, use a RemoteEvent:
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()
end)
local RemoteEv = game.ReplicatedStorage:WaitForChild("RemoteEvent")
game.Players.LocalPlayer.Character.Humanoid.Jumping:connect(function()
RemoteEv:FireServer()
end)
2 comments
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?
Since I have a keydown script it has to be local but i also want the keydown script to play an animation.