I am trying to make an system that will choose one player has ghost and rest of players becomes survivors apparently, instead of making this, my script chooses to put all players has ghosts, which is weird, can someone help me fix this issue? I already tried to put break on the end of ghost for loop .
local Players = game:GetService("Players")
local Physics = game:GetService("PhysicsService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")
local SoundService = game:GetService("SoundService")
local Settings = ReplicatedStorage:WaitForChild("Settings")
local RoleGhost = Settings:WaitForChild("RoleGhost").Value
local RoleSurvivor = Settings:WaitForChild("RoleSurvivor").Value
local RoleNone = Settings:WaitForChild("RoleNone").Value
function setRolesForPlayers()
local Choosen = 0
local Group = Players:GetChildren()
Choosen = math.random(1, #Group)
-- Clear all roles
for i, v in pairs(Group) do
if Workspace:FindFirstChild(v.Name) then
if Workspace[v.Name]:FindFirstChild("Role") then
if Workspace[v.Name].Role.Value then
Workspace[v.Name].Role.Value = RoleNone
print(Workspace[v.Name].Role.Value)
end
end
end
end
-- Set survivor role
for i, v in pairs(Group) do
if Workspace:FindFirstChild(v.Name) then
if Workspace[v.Name]:FindFirstChild("Role") then
if Workspace[v.Name].Role.Value then
if Workspace[v.Name].Role.Value == RoleNone or RoleSurvivor then
Workspace[v.Name].Role.Value = RoleSurvivor
print(v.Name.. "is now a ".. RoleSurvivor)
else
print("Can't be survivor")
end
end
end
end
end
-- Set ghost role
for i, v in pairs(Group) do
if i == Choosen then
if Workspace:FindFirstChild(v.Name) then
if Workspace[v.Name]:FindFirstChild("Role") then
if Workspace[v.Name].Role.Value then
if Workspace[v.Name].Role.Value == not RoleGhost then
Workspace[v.Name].Role.Value = RoleGhost
print(v.Name.. "is now a ".. RoleGhost)
break
end
end
end
end
end
end
end
while wait(3) do
local Group = Players:GetChildren()
if #Group >= 2 then
setRolesForPlayers()
print("Worked")
end
end
Also I didn’t get any errors, or warnings… I tried debugging too…
I made similar system before, this never happened to me, should I use game.Players instead of local Players = game:GetService("Players")? I don’t understand why break doesn’t actually break
-- Set ghost role
for i, v in pairs(Group) do
print("Try to make ghost")
if i == Choosen then
print("Yes, that player is choosen as ghost")
if Workspace:FindFirstChild(v.Name) then
print("Exists in workspace!")
if Workspace[v.Name]:FindFirstChild("Role") then
print("Role exits!")
if Workspace[v.Name].Role.Value then
print("Yes, rule is value")
if Workspace[v.Name].Role.Value == not RoleGhost then -- Here script detects its not ghost but still makes player ghost for weird reson ;/
print("Player is not ghost!")
Workspace[v.Name].Role.Value = RoleGhost
print(v.Name.. "is now a ".. RoleGhost)
break
end
end
end
end
end
end
I believe the reason you’re experiencing an issue is because you set Choosen for all players rather than per player, due to this everyone is picked as a ghost.
local Players = game:GetService("Players")
local Physics = game:GetService("PhysicsService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")
local SoundService = game:GetService("SoundService")
local Settings = ReplicatedStorage:WaitForChild("Settings")
local RoleGhost = Settings:WaitForChild("RoleGhost").Value
local RoleSurvivor = Settings:WaitForChild("RoleSurvivor").Value
local RoleNone = Settings:WaitForChild("RoleNone").Value
function setRolesForPlayers()
local Chosen = 0
local Group = Players:GetChildren()
Chosen = Group[math.random(1, #Group)]
-- Clear all roles
for i, v in pairs(Group) do
if Workspace:FindFirstChild(v.Name) then
if Workspace[v.Name]:FindFirstChild("Role") then
if Workspace[v.Name].Role.Value then
Workspace[v.Name].Role.Value = RoleNone
print(Workspace[v.Name].Role.Value)
end
end
end
end
-- Set ghost role
if Chosen.Character:FindFirstChild("Role") then
if Chosen.Character.Role.Value == not RoleGhost then
Chosen.Character.Role.Value = RoleGhost
print(Chosen.Name.. "is now a ".. RoleGhost)
end
end
-- Set survivor role
for i, v in pairs(Group) do
if Workspace:FindFirstChild(v.Name) then
if Workspace[v.Name]:FindFirstChild("Role") then
if Workspace[v.Name].Role.Value then
if Workspace[v.Name].Role.Value == RoleNone or RoleSurvivor then
Workspace[v.Name].Role.Value = RoleSurvivor
print(v.Name.. "is now a ".. RoleSurvivor)
else
print("Can't be survivor")
end
end
end
end
end
end
while wait(3) do
local Group = Players:GetChildren()
if #Group >= 2 then
setRolesForPlayers()
print("Worked")
end
end
I believe I’ve come up with a solution. If this does not work It would be appreciated if you could provide an .rbxl file.
Here is a solution that I believe will work:
local Players = game:GetService("Players")
local Physics = game:GetService("PhysicsService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")
local SoundService = game:GetService("SoundService")
local Settings = ReplicatedStorage:WaitForChild("Settings")
local RoleGhost = Settings:WaitForChild("RoleGhost").Value
local RoleSurvivor = Settings:WaitForChild("RoleSurvivor").Value
local RoleNone = Settings:WaitForChild("RoleNone").Value
function setRolesForPlayers()
local Chosen = 0
local Group = Players:GetChildren()
Chosen = Group[math.random(1, #Group)]
-- Clear all roles
for i, v in pairs(Group) do
if Workspace:FindFirstChild(v.Name) then
if Workspace[v.Name]:FindFirstChild("Role") then
if Workspace[v.Name].Role.Value then
Workspace[v.Name].Role.Value = RoleNone
end
end
end
end
-- Set ghost role
if Chosen.Character:FindFirstChild("Role") then
if Chosen.Character.Role.Value ~= RoleGhost then
Chosen.Character.Role.Value = RoleGhost
print(Chosen.Name.. "is now a ".. RoleGhost)
end
end
-- Set survivor role
for i, v in pairs(Group) do
if Workspace:FindFirstChild(v.Name) then
if Workspace[v.Name]:FindFirstChild("Role") then
if Workspace[v.Name].Role.Value then
if (Workspace[v.Name].Role.Value == RoleNone or RoleSurvivor) and (Workspace[v.Name].Role.Value ~= RoleGhost) then
Workspace[v.Name].Role.Value = RoleSurvivor
print(v.Name.. "is now a ".. RoleSurvivor)
else
print("Can't be survivor")
end
end
end
end
end
end
while wait(3) do
local Group = Players:GetChildren()
if #Group >= 2 then
setRolesForPlayers()
print("Worked")
end
end
This is what I used. Settings all of them are strings that are with value “Ghost”, “Survivor” and “None”. All players start with “None”. GameManager is the script I am talking about. Nothing else…
You’re not using the new solution I’ve provided.
local Players = game:GetService("Players")
local Physics = game:GetService("PhysicsService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")
local SoundService = game:GetService("SoundService")
local Settings = ReplicatedStorage:WaitForChild("Settings")
local RoleGhost = Settings:WaitForChild("RoleGhost").Value
local RoleSurvivor = Settings:WaitForChild("RoleSurvivor").Value
local RoleNone = Settings:WaitForChild("RoleNone").Value
function setRolesForPlayers()
local Chosen = 0
local Group = Players:GetChildren()
Chosen = Group[math.random(1, #Group)]
-- Clear all roles
for i, v in pairs(Group) do
if Workspace:FindFirstChild(v.Name) then
if Workspace[v.Name]:FindFirstChild("Role") then
if Workspace[v.Name].Role.Value then
Workspace[v.Name].Role.Value = RoleNone
end
end
end
end
-- Set ghost role
if Chosen.Character:FindFirstChild("Role") then
if Chosen.Character.Role.Value ~= RoleGhost then
Chosen.Character.Role.Value = RoleGhost
print(Chosen.Name.. "is now a ".. RoleGhost)
end
end
-- Set survivor role
for i, v in pairs(Group) do
if Workspace:FindFirstChild(v.Name) then
if Workspace[v.Name]:FindFirstChild("Role") then
if Workspace[v.Name].Role.Value then
if (Workspace[v.Name].Role.Value == RoleNone or RoleSurvivor) and (Workspace[v.Name].Role.Value ~= RoleGhost) then
Workspace[v.Name].Role.Value = RoleSurvivor
print(v.Name.. "is now a ".. RoleSurvivor)
else
print("Can't be survivor")
end
end
end
end
end
end
while wait(3) do
local Group = Players:GetChildren()
if #Group >= 2 then
setRolesForPlayers()
print("Worked")
end
end
Hey, it would be much more helpful if you are able to provide your game in a file, if you cannot do this, it is fully understandable. It is currently confusing what the issue is, as what @CleverSource provided should be working.