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 error?

Asked by 4 years ago

I am making a murder game or something like that. When I make an event, it doesn't work and gives the error " 18:41:06.569 - Unable to cast value to Object". Here is the code. 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")

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)

Answer this question