I'm trying sending a function to client using remote events

My game is Spell game, since players will have differents skills and i made a skill bar.

And skills have differents animations, so i will need send the animations for the client using remote events (since is better active the animations on client), however some skills I did have several animations, so I activate the animations in order using Track.Stopped and some skills no, so instead of just sending the animations, I wanted to send a function that will control the animations.

    local function playTrack(Character,Id,Id2)
	print("FUNCTIO NWORKING LMAO")
	local Animation = Instance.new("Animation")
	Animation.AnimationId = Id
	local Track = Character:WaitForChild("Humanoid"):LoadAnimation(Animation)
	Animation.AnimationId = Id2
    local Track2 = Character:WaitForChild("Humanoid"):LoadAnimation(Animation)

    Track.Stopped:Connect(function()
	     Track2:Play()
	end)
		
	Track:Play()	
end

SendAnimation:FireClient(player,playTrack,{Character,"rbxassetid://4717948056","rbxassetid://4718138141"})

The client receive

    local SendAnimation = game:GetService("ReplicatedStorage").Remotes.SendAnimation

local function playT(trackF,Args)
	trackF(unpack(Args))
end


SendAnimation.OnClientEvent:Connect(playT)

But dont work https://media.discordapp.net/attachments/462394160629153812/701842368000819250/unknown.png?width=722&height=406

I dont have idea how fix that, i never sended a function to a client, anyone can help?

References to functions cannot be sent over because they do not serialize. They could be serialized but the effort required to serialize them yields no usefulness. But I must ask; why do you want to send a function???

2 Likes

It’s not working because you cannot send functions through Remotes. I’d recommend you have the function in the client script instead of the server, as it’s not even used in the server-script.

Here’s a quick fix

--server
sendAnimation:FireClient(player,{Character,"rbxassetid://4717948056","rbxassetid://4718138141"})
-- client
local SendAnimation = game:GetService("ReplicatedStorage").Remotes.SendAnimation
local function playTrack(Character,Id,Id2)
	print("FUNCTIO NWORKING LMAO")
	local Animation = Instance.new("Animation")
	Animation.AnimationId = Id
	local Track = Character:WaitForChild("Humanoid"):LoadAnimation(Animation)
	Animation.AnimationId = Id2
    local Track2 = Character:WaitForChild("Humanoid"):LoadAnimation(Animation)

    Track.Stopped:Connect(function()
	     Track2:Play()
	end)
		
	Track:Play()	
end

SendAnimation.OnClientEvent(function(Args)
    playTrack(unpack(Args))
end)

Edit: If you really, really need to send functions through RemoteEvents/RemoteFunctions, you could write the code as a string, and have the client execute it through either loadstring or EpicLua. I’d highly suggest against this though.

I think you can but you gotta put function into table and then send table to remote event

That does not work either. You cannot send a function at all.

1 Like

because my skills have several animations, I put events in them or use Stopped to activate the next animation, could I leave this function in localscript? NO, because not everyone will have this skills, since it is an RPG type game, so I’m trying to do something that will work with any skills, something generic

I don’t think functions can be sent through RemoteEvents/RemoteFunctions, even if they are in a table.

Okay fine, you got my point I guess, I don’t want to open my studio and it’s almost my bedtime

Then only fire to the players with the skills?? I do not understand your point.

But I think I remember creating tower defense and then send table with Functions to remote and it actually save but I’m not sure

If you want to make sure the client isn’t playing the animations without having the appropriate criteria, by all means you can send a RemoteFunction/Event to the server confirming if the player does or does not have the ability (via Data recorded by DataStore/ Objects) to use this exclusive skill.

Even if they ran the animations and skills locally, the server would not acknowledge it (unless you have a bad client-server implementation of FE)

Functions cannot be serialized and sent through Remotes.

@TenerPVPs

A work around however, would be to place the function in a Module Script on the Client and require it to run the function.

You can also enable Loadstring and send scripts from the server to the Client but this option is risky because it allows Clients to run dynamic code where the strings can run as code on the Server, sent through Remote Functions.

You can’t send functions to the client. If you send an array such as this:

{
[1] = (function() print("Hello World!") end);
[2] = "Hello World!";
}

The client will get this:

{
[1] = "Hello World!";
}

You also have to be careful sending any type of array with gaps to the client,

{
[1] = "Hello World!";
[2] = Color3.new();
}

will work fine, but

{
[2] = "Hello World!";
[3] = Color3.new()
};

will give you a really nasty error sometimes that is hard to address unless you have already thought about it before coding. “Invalid Table Key Type”

so i will need active the animation on server, is my only solution

it will not work, first because the skill is on the server, and I will only continue the skill when the animation arrives at a certain frame, if I am activating it on the client there is no way for me to check on the server