Unable to cast value to Object error?

I am making a murder type of game, and it shows the players their roles with a GUI. But the GUI will not show their roles. The error is " [ Unable to cast value to Object]". I am using a remote event as well.
Code for script.

--SpyxSpy main script
local SpyKit = game.ReplicatedStorage.Sword

local MinPlayers = script.MinPlayers

local MinPlayersValue = script.MinPlayers.Value

local spawns = {"Spawn1", "Spawn2", "Spawn3", "Spawn4", "Spawn5", "Spawn6"}

local selectedSpawn = spawns[math.random(1, #spawns)]

local Teleport = selectedSpawn

local spawn = workspace[selectedSpawn]

local players = game.Players:GetPlayers()

local roles = { --our roles table, where the name means the maximum members
    {Name = "Scientist", Max = 6},
    {Name = "Spy", Max = 1},
    {Name = "Soldier", Max = 2}
}
local playerRoles = {} --will store what players have what roles

function getRoleMembers(roleName) --returns a table of players that have a role
    local playerTable = {}
    for player,role in pairs(playerRoles) do
        if role == roleName then
            playerTable[#playerTable + 1] = player
        end
    end
    return playerTable
end

function getRolesFull() --checks if all the roles are full
    for _,roleTbl in pairs(roles) do
        if #getRoleMembers(roleTbl.Name) < roleTbl.Max then
            return false
        end
    end
    return true
end

function getRole() --get a random role name that isnt full
    if getRolesFull() then
        return nil
    end
    local chosen
    repeat
        chosen = roles[math.random(1,#roles)]
    until #getRoleMembers(chosen.Name) < chosen.Max
    return chosen.Name
end

for _,player in pairs(players) do --select each player's role
    playerRoles[player] = getRole()
end
--if there are no available roles, remaining players will not receive one
local GUIEvent = game.ReplicatedStorage.RemoteEvents.GUIEvent
local selectedRole = playerRoles[players]
GUIEvent:FireClient(players,selectedRole)
print("frostbite") ---Used to see if it was working, it did not print.

local script

local remoteEvent = game.ReplicatedStorage:WaitForChild("GUIEvent")
local Player = game.Players.PlayerAdded
.OnClientEvent(function(selectedRole)
	if selectedRole == "Scientist" then
    	game.StarterGui.ClassGui.Frame.TextLabel.TextSize = 50
    	game.StarterGui.ClassGui.Frame.TextLabel.Text = "Scientist, stick with soldiers!"
    	wait(2)
    	game.StarterGui.ClassGui.Frame.Visible = false
	elseif selectedRole == "Soldier" then
    	game.StarterGui.ClassGui.Frame.TextLabel.TextSize = 50
    	game.StarterGui.ClassGui.Frame.TextLabel.Text = "Soldier, protect scientists against the spy!"
    	wait(2)
    	game.StarterGui.ClassGui.Frame.Visible = false
	elseif selectedRole == "Spy" then
    	game.StarterGui.ClassGui.Frame.TestLabel.TextSize = 50
    	game.StarterGui.ClassGui.Frame.TextLabel.Text = "Spy, kill soldiers and scientists, and gather data!"
    	wait(2)
    	game.StarterGui.ClassGui.Frame.Visible = false
	end
end)

Can you tell me what line the error is occuring on?

This could be it.

local remoteEvent = game.ReplicatedStorage:WaitForChild("GUIEvent")
local Player = game.Players.PlayerAdded
.OnClientEvent(function(selectedRole)

Because you just have .OnClientEvent it’s trying to access game.Players.PlayerAdded on the line before to call the .OnClientEvent on.
Secondly, you are doing .OnClientEvent(function() and not .OneClientEvent:Connect(function()

1 Like

this line the second to last one

that still didn’t work with no errors

GUIEvent, is an object there.

The thing is, you’re trying to fire the event to players, which is a table of players.
If you’re trying to send it to all clients, do FireAllClients.

If you’re trying to fire certain players, make it loop through the players table and then fire those players individually.

1 Like

I did

local remoteEvent = game.ReplicatedStorage:WaitForChild("GUIEvent")
local Player = game.Players.PlayerAdded
.OnClientEvent(function(selectedRole)
	remoteEvent:FireAllClients(players,selectedRole)
end)

which did not work either

What line is the error on? Because 1. local selectedRole = playerRoles[players] --players is a table, not one player.
2. GUIEvent:FireClient(players,selectedRole) --selectedRole is nil, players, again is a table.

Just do GUIEvent:FireAllClients(playerRoles), and have the client find their role from there.

1 Like

I did that, but it doesn’t work again and for some reason no error

  1. You don’t have anything before OnClientEvent. Did you mean remoteEvent.OnClientEvent?
  2. You don’t have Connect. OnClientEvent is an event, not a method.
  3. FireAllClients cannot be used from the client.

Please do a bit of reading on the Developer Hub, it’ll help to fill some gaps of knowledge for how to use these things properly. There are also code samples there that you can reference.

3 Likes

You were trying to send an event to people in a table, which you can’t do. You can only send events to everyone or one person. To work around this, use “for i,v in pairs(players) do end”.