Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Unable to cast value to Object?

So, I'm trying to do a Gui that shows up to all the players when someone (an HR from my group) clicks another button. I get an error from the Script that is inside of the Gui, that says "Unable to cast value to Object" https://gyazo.com/8d6591d0cd133a22e61f5f0711b0e298

local RemoteEvent = game.ReplicatedStorage.AdvertEvent

function Click()
	local Player = script.Parent.Parent.Parent.Parent.Parent.Name
    RemoteEvent:FireClient(Player)
end

script.Parent.MouseButton1Click:Connect(Click)

I have a LocalScript inside StarterPlayer > StarterPlayerScripts > Events > LocalScript that puts:

local AdvertEvent = game.ReplicatedStorage.AdvertEvent
local Player = game.Players.LocalPlayer

function ShowAdvert()
    local Gui = game.StarterGui.AdvertGui
    if (not Player.PlayerGui:FindFirstChild(Gui.Name)) then
        Gui.Parent = Player.PlayerGui
    end
end

AdvertEvent.OnClientEvent:Connect(ShowAdvert)

4 comments

0

Casting means converting something of one datatype into another datatype. In your first script at line 4 I'm assuming you're getting the name of the Player.. FireClient takes a Player object for its argument. The Name property gets a string, not a Player, therefor an invalid cast. Use FireClient and pass an actual Player, not a string.

0

Now I get the error "Argument 1 missing or nil" in line 5. Maybe because it should be a LocalScript (becuase it's inside a Gui)

0

FireClient can only be used in server-scripts. That's what server-client communication is. Argument 1 is nil because script.Parent.Parent.Parent.Parent.Parent is nil.

0

Now it says "FireClient: player argument must be a Player object" (I removed one ".Parent")

1 answer

You can only call FireClient from the server. When the MouseButton1Click event is fired, call FireServer and then from the server, call FireClient.

This discussion is preserved as it was. New answers, comments, and votes are closed.