Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How would I fix this script for a murder-like game?

Below I have a script that ends the game depending on the circumstances.

A Murderer is chosen, all players are teleport-ed to the map and it begins. All the players other than the murderer are put into a table called _G.AllPlayers and once a player dies they get removed from the table. The problem is, as soon as the game starts it says the murderer is victorious...

Please help I literally don't know what's wrong with it.


repeat
			wait(1)
			GameTime = GameTime - 1
			game.ServerScriptService.LightsOutPerk.Disabled = false
			CountDown:FireAllClients('StartCountDown',GameTime.." SECONDS REMAINING")
			while true do
				if _G.Murderer == nil and #_G.AllPlayers > 0 then
					win = 3
					break
				elseif _G.Murderer == nil and #_G.AllPlayers == 0 then
					win = 2
					break
				elseif _G.Murderer and #_G.AllPlayers == 0 then
					win = 1
					break
				elseif GameTime == 0 then
					win = 3
					break
				end
			end
		until win == 1 or win == 2 or win == 3

1 answer

Your while true do loop should be freezing your place - fortunately for you, one of your variables isn't set up correctly and one of your if branches is running, setting win to one of those values and terminating the outer loop. Delete the while true do part (ie lines 6 and 20 -- keep 7-19 of course) and print out the values of _G.Murderer, #_G.AllPlayers, and GameTime. One of them is probably not what you expect. If they all look good, print out which 'if' statement is running (ie put a print in each branch) and keep trying to figure out what your script is doing - eventually you'll find how it's different than you expect.

To make sure your script is easy to understand and change, you should use constants instead of numbers when representing concepts, ex:

local gameOn = 0
local murdererWins = 1
local playersWin = 2
local draw = 3

--ex:
elseif GameTime == 0 then
	win = draw
end
--is much clearer than "win = 3"

This discussion is preserved as it was. New answers, comments, and votes are closed.