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

How do I get this script to work for a murder game?

I went to script analysis and I fixed everything including warnings, yet it still doesn't work.

local event = replicatedstorage:WaitForChild("RemoteEvent")
local maps = serverstorage:WaitForChild("Maps")
local mapholder = game.Workspace:WaitForChild("MapHolder")


while true do
	print 'Got Passed'
	-- Load random map
	mapholder:ClearAllChildren()
	print 'Cleard!'
	wait(2)
	local allmaps = maps:GetChildren()
	local newmap = allmaps[math.random(1, #allmaps)]
	newmap.Parent = game.Workspace
	wait(2)
	print 'New Map'
	-- Wait for contestants
	while true do
		wait(5)
		contestants = {}
		for _, player in pairs(game.Players:GetPlayers()) do
			if player and player.Character then
				local humanoid = player.Character:WaitForChild("Humanoid")
				if humanoid and humanoid.Health > 0 then
					table.insert(contestants, player)
				end
			end
		end
		if #contestants >= 2 then
			break
	end
end
-- Choose Murderer
	murderer = contestants[math.random(1, #contestants)]
-- Choose Sheriff
	while true do
		randomplayer = contestants[math.random(1, #contestants)]
		if randomplayer ~= murderer then
			sheriff = randomplayer
			break
	end
-- Teleport Contestants
	local spawnsmodel = newmap:WaitForChild("Spawns")
	local spawns = spawnsmodel:GetChildren()
	for _, player in pairs(contestants) do
		if player and player.Character and #spawns > 0 then
			local torso = player.Character:WaitForChild("Torso")
			local spawnindex = math.random(1, #spawns)
			local spawn = spawns[spawnindex]
			if spawn and torso then
				table.remove(spawns, spawnindex)
				torso.CFrame = CFrame.new(spawn.Position + Vector3.new(0, 3, 0))
				
				local matchtag = Instance.new("StringValue")
				matchtag.Name = "MatchTag"
				matchtag.Parent = player.Character
				
				local backpack = player:FindFirstChild("Backpack")
				if backpack then
					if player == murderer then
						local knife = serverstorage:WaitForChild("Knife"):clone()
						knife.Parent = backpack
					elseif player == sheriff then
						local pistol = serverstorage:WaitForChild("Pistol"):clone()
						pistol.Parent = backpack
					end
				end
			end
		end
	end
	spawnsmodel:remove()
-- Wait for round end conditions
local localtimer = roundtimer
while localtimer > 0 do
	wait(1)
	localtimer = localtimer -1
	activecontestants = {}
	murdereractive = false
	for _, player in pairs(contestants) do
		if player then
			local character = player.Character
			if character then
				local matchtag = player.Character:FindFirstChild("MatchTag")
				local humanoid = character:FindFirstChild("Humanoid")
				if matchtag and humanoid and humanoid.Health > 0 then
						if player == murderer then
							murdereractive = true
						end
				
						table.insert(activecontestants, player)
					end
				end
			end
		end
		if #activecontestants <= 1 or not murdereractive then
			break
		end
	end
-- Game Results

	local gameresults = "PlayersWin"
	if murdereractive then
		if #contestants > 2 then
			-- Murderer failed
		else
			-- Murderer won
			gameresult = "MurdererWins"
		end
	else
		-- Murderer died
	end
	
	-- Teleport Players Back To Lobby
	
	local lobbyspawns = {}
	for _, v in pairs(game.Workspace:WaitForChild("Lobby"):GetChildren()) do
		if v and v.name == "SpawnLocation" then
			table.insert(lobbyspawns, v)
		end
	end
	for _, player in pairs(activecontestants) do
		if player then
			if player.Character then
				local humanoid = player.Character:FindFirstChild("Humanoid")
				if humanoid then
					humanoid:UnequipTools()
				end
			end
			local randomspawn = lobbyspawns[math.random(1, #lobbyspawns)]
			player.Character:MoveTo(randomspawn.Postion)
			local backpack = player:FindFirstChild("Backpack")
			if backpack then
				backpack:ClearAllChildren()
			end
		end
	end
end
	wait()
	print 'Done'
end


4 comments

0

Can you please include the output or the error text?

0

There is no error text.

0

20:35:12.252 - ServerScriptService.MainScript:51: attempt to call method 'WaitForChild' (a nil value) 20:35:12.254 - Stack Begin 20:35:12.257 - Script 'ServerScriptService.MainScript', Line 51 20:35:12.263 - Stack End20:35:50.785 - ServerScriptService.MainScript:21: bad argument #2 to 'random' (interval is empty) 20:35:50.796 - Script 'ServerScriptService.MainScript', Line 21 20:35:50.798 - Stack

1

hi mnaj

1 answer

Well look here:

local event = replicatedstorage:WaitForChild("RemoteEvent")
local maps = serverstorage:WaitForChild("Maps")

It should be like this:

local event = game.ReplicatedStorage:FindFirstChild("RemoteEvent")
local maps = game.ServerStorage:FindFirstChild("Maps")

Hope this works.

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