Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Why does an InvokeClient always return this error?

Asked by
Fifkee 2017 Community Moderator Moderation Voter
6 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
    print(amount)
    game:GetService('ReplicatedStorage').Remotes.addMoney:InvokeServer(tostring(of.GetHash()), tonumber(amount))
end

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
1
Answered by
Avigant 2374 Moderation Voter Community Moderator
6 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: http://wiki.roblox.com/index.php?title=API:Class/RemoteFunction/InvokeClient

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.

0
greatest of all time Fifkee 2017 — 6y
Ad

Answer this question