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)