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

Unable to cast value to Object?

Asked by
h2wk 0
5 years ago

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)
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. pidgey 548 — 5y
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) h2wk 0 — 5y
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. pidgey 548 — 5y
0
Now it says "FireClient: player argument must be a Player object" (I removed one ".Parent") h2wk 0 — 5y

1 answer

Log in to vote
1
Answered by
mado 40
5 years ago

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

Ad

Answer this question