Still have questions? Join our Discord server and get real time help.
Log in to vote

Why does an InvokeClient always return this error?

Asked by
Fifkee 2017 Administrator Moderation Voter Community Moderator
2 years ago

17:38:46.671 - Unable to cast value to Object

My worst enemy, right there. The problem is, that no matter what I do to the invokeclient, it either returns nil, or that error.

game.Players[script.Parent.Parent.Parent.Name].PlayerGui.mainGame['no hackie 4 u']:InvokeClient(nil, tonumber(otherPart.Value.Value))

What makes it return nil, you say?

script.GiveMoney.OnClientInvoke = function(player, amount)
    if not amount then print'nil' return nil end
    game:GetService('ReplicatedStorage').Remotes.addMoney:InvokeServer(tostring(of.GetHash()), tonumber(amount))

For some reason, amount is nil, even though it's CLEARLY there. (otherPart.Value.Value)

Don't worry about otherPart. The value provided is 1.

If I were to do :InvokeClient(tonumber(otherPart.Value.Value), it would return the error above.

Any ideas?

1 answer

Log in to vote
Answered by
Avigant 2374 Moderation Voter Community Moderator
2 years ago

It is not safe to invoke a RemoteFunction to the client, as they can refrain from ever yielding and this will make your server wait indefinitely. You could invoke the client in a new thread in theory, but using RemoteEvents is a much nicer solution.

The server can't access children of a player's PlayerGui, by the way, unless they parented it. The server should never know or care about any ScreenGui.

You're also forgetting that the first argument to RemoteFunction:InvokeClient() needs to be a player to invoke:

Furthermore, it is dangerous to have an event such as "addMoney", or seems that way from its name. You should never trust the client, and certainly never let them increase their money arbitrarily.

I'm not quite sure what you're trying to do in the second part of your code, it seems like doing this server-side without any remote would be much better as a solution.

greatest of all time Fifkee 2017 — 2y

Answer this question