Hello! I’m currently working on my first “real game” and I am trying to make a system that will assign players a role value at the end of every “shift”. However, it doesn’t seem to be doing anything except for restart the shift times. I’ve messed around with it for a while, and nothing similar seems to be on the forum. I put some prints in, and nothing is happening when it absolutely should. The non-working code will be down below. Note that all variables are defined, i’m just putting the messy stuff.
–serversided start shift function
local function StartShift()
local playercount = Instance.new("IntValue")
playercount.Value = 0
--reset shift stats
shifttime.Value = 1800
midshift.Value = true
local function GetPlayerCount()
for i,v in ipairs(players:GetChildren()) do
playercount.Value = playercount.Value + 1
end
print("There are "..playercount.Value.." players")
end
local function RandomizeRoles()
local operators = Instance.new("IntValue")
operators.Value = 0
local engineers = Instance.new("IntValue")
engineers.Value = 0
local security = Instance.new("IntValue")
security.Value = 0
local manager = Instance.new("IntValue")
manager.Value = 0
for i,v in ipairs(players:GetChildren()) do
local player = v
local character = player.Character or player.CharacterAdded:Wait()
GetPlayerCount()
if manager.Value == 0 then
character.Humanoid.Role.Value = "manager"
print("Operator")
elseif security.Value >= playercount/10 then
character.Humanoid.Role.Value = "security"
print("Operator")
elseif engineers.Value >= playercount/25 then
character.Humanoid.Role.Value = "engineer"
print("Operator")
else
character.Humanoid.Role.Value = "operator"
print("Operator")
end
end
operators:Destroy()
engineers:Destroy()
security:Destroy()
manager:Destroy()
end
RandomizeRoles()
replicatedstorage.Events.ShiftStart:Fire()
print("Shift Started")
playercount:Destroy()
end
–clientsided shift manager (manages a shift timer, using it to make sure the client receives that they are that role. just pasting the printrole part linked to the broken server script)
local function printrole()
print("You are the "..localplayer.Character.Humanoid.Role.Value)
end
replicatedstorage.Events.ShiftStart.Changed:Connect(printrole)
If you need any more information please let me know!
Infinite Loop in GetPlayerCount: The GetPlayerCount function is designed to count the players in the players container, but it is called within the loop in the RandomizeRoles function. This could lead to an infinite loop since GetPlayerCount increases playercount.Value, and this change might trigger the loop again.
here is a snippet of the function:
local function GetPlayerCount()
playercount.Value = #players:GetChildren()
print("There are " .. playercount.Value .. " players")
end
Incorrect Print Statements: The print("Operator") statement is used in all branches of your role assignment logic. It might be a copy-paste mistake. You may want to replace these with accurate print statements.
Roles Assignment Logic: The role assignment logic might not be achieving the desired distribution of roles. For example, the condition security.Value >= playercount/10 seems to be comparing an integer with a float. It would be better to use security.Value >= playercount.Value / 10. Also, ensure that roles are assigned correctly based on your intended logic.
RandomizeRoles Function
local function RandomizeRoles()
local operators = 0
local engineers = 0
local security = 0
local manager = 0
for _, player in ipairs(players:GetChildren()) do
local character = player.Character or player.CharacterAdded:Wait()
GetPlayerCount()
if manager == 0 then
character.Humanoid.Role.Value = "manager"
print("Manager")
manager = manager + 1
elseif security < playercount.Value / 10 then
character.Humanoid.Role.Value = "security"
print("Security")
security = security + 1
elseif engineers < playercount.Value / 25 then
character.Humanoid.Role.Value = "engineer"
print("Engineer")
engineers = engineers + 1
else
character.Humanoid.Role.Value = "operator"
print("Operator")
operators = operators + 1
end
end
end
Here is Both the Server script and client
Serverscript:
local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local playercount = Instance.new("IntValue")
local function GetPlayerCount()
playercount.Value = #players:GetPlayers()
print("There are " .. playercount.Value .. " players")
end
local function RandomizeRoles()
local operators = 0
local engineers = 0
local security = 0
local manager = 0
for _, player in ipairs(players:GetPlayers()) do
local character = player.Character or player.CharacterAdded:Wait()
GetPlayerCount()
if manager == 0 then
character.Humanoid.Role.Value = "manager"
print("Manager")
manager = manager + 1
elseif security < playercount.Value / 10 then
character.Humanoid.Role.Value = "security"
print("Security")
security = security + 1
elseif engineers < playercount.Value / 25 then
character.Humanoid.Role.Value = "engineer"
print("Engineer")
engineers = engineers + 1
else
character.Humanoid.Role.Value = "operator"
print("Operator")
operators = operators + 1
end
end
end
local function StartShift()
playercount.Value = 0
shifttime.Value = 1800
midshift.Value = true
RandomizeRoles()
replicatedStorage.Events.ShiftStart:Fire()
print("Shift Started")
playercount:Destroy()
end
StartShift()
Client script:
local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local localPlayer = players.LocalPlayer
local function PrintRole()
if localPlayer.Character then
print("You are the " .. localPlayer.Character.Humanoid.Role.Value)
end
end
replicatedStorage.Events.ShiftStart.Changed:Connect(PrintRole)
Tried this fix, and my shift timer and stuff is still running, but RandomizeRoles and GetPlayerCount are still not doing anything, No prints, no errors, and the client also isn’t doing anything.
Do i need a RemoteEvent instead of a BindableEvent for client?
Just gonna bump this. I’ve been looking at it for a minute, and have no idea why it won’t work. It fires the client event before shift start prints, yet i don’t get the client sided print. Just the shift start print