Making an system that will choose one ghost and all other people becomes survivors, but instead it chooses all people as ghosts!

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…

3 Likes

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

You have any idea why break doesn’t breaks the for loop, Since I used break to stop the for loop so it does’t try put other players as ghosts too.

-- 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
1 Like

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.

The Choosen doesn’t change, so I have no idea. It only changes at the first few lines of script in function

Can you provide an .rbxl file?

Let me try something else, I will respond with more information in few minutes

This should possibly 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
					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

You also want to set the “ghost” first.

1 Like

Annotation%202019-09-28%20133140

It still shows the same thing

It would be helpful if you could provide an .rbxl file like I’ve stated.

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
1 Like


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…

Can you attempt to use my method that I have provided?

1 Like

I tried, nothing changed, all players are ghosts, no matter what.

Are you sure? I’ve tested this myself and it worked. I provided a new solution above.

This is the output I receive when using my method:
image

1 Like
Output says: Worked
Output says: None(x2)
Output says: Player 1 is ghost
Output says: Player 2 is ghost
Output says: Player 3 is ghost

I wonder why…

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
3 Likes


Proof that I used your solution, and didn’t worked…

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.

2 Likes