Attempt to call a nil value (function)

idk how to explain but i have a function but idk why it still can’t get the function. i think the function startGameTimer() trying to get the startIntermission() but the startIntermission() doesnt exist yet since i place the startGameTimer() on top. Even if i switch the place between these ofc it will be the same. so the point here i want to make after the startGameTimer() end, it will automatically turn to startIntermission(). (like a loop never stop cycle)

local function startGameTimer()
	gameActive = true
	for i = gameTime, 0, -1 do
		timer:FireAllClients("GAME TIME LEFT: " .. i .. " seconds")

		if i % 5 == 0 then
			local zombies = 0
			local survivors = 0

			for _, player in pairs(pl:GetPlayers()) do
				if player.Team == game.Teams.Zombie then
					zombies = zombies + 1
				elseif player.Team == game.Teams.Survivor then
					survivors = survivors + 1
				end
			end

			if zombies == 0 then
				timer:FireAllClients("Survivors Win!")
				task.wait(3)
				gameActive = false
				break
			elseif survivors == 0 then
				timer:FireAllClients("Zombies Win!")
				task.wait(3)
				gameActive = false
				break
			end
		end

		task.wait(1)
	end

	if gameActive then
		timer:FireAllClients("Time's up! It's a Draw!")
		task.wait(3)
	end

	for _, player in pairs(pl:GetPlayers()) do
		player.Team = teamw
		local rootPart = player.Character and player.Character:FindFirstChild("HumanoidRootPart")
		if rootPart and game.Workspace:FindFirstChild("Part") then
			rootPart.CFrame = game.Workspace.Part.CFrame * CFrame.new(0, 1, 0)
		end
	end

	startIntermission()
end

local function startIntermission()
	while true do
		if #pl:GetPlayers() > 1 then 
			for i = timertime, 0, -1 do
				timer:FireAllClients("INTERMISSION: " .. i .. " seconds left")
				if audiop then audiop:Play() end
				task.wait(1)
			end

			timer:FireAllClients("PREPARE FOR BATTLE!")
			task.wait(1)

			teleportPlayers()

			task.wait(3)
			startGameTimer()

			break
		else
			timer:FireAllClients("Waiting for more players...")
			task.wait(3)
		end
	end
end

task.spawn(function()
	startIntermission()
end)

just for testing will it work if u remove the local keyword when defining the functions?
like function startIntermission(a) instead of local function startIntermission(a)

1 Like

this should help: Any advantage in declaring a function as local? - #30 by nicemike40

you can just declare them on top and it will work:

local startGameTimer, startIntermission

function startGameTimer()
--your code
end

function startIntermission()
--your code
end

1 Like

Create a StringValue “GameTimer” in ReplicatedStorage

local timer = {}
timer.__index = module
 
function timer:Stop()
 self.IsStopped = true
end
 
function timer:Play(duration: number, callback: (A...) -> (), ...: A...): ()
 duration = duration or 10
 self.IsStopped = false
 local startTime = tick() + duration
 
 while true do
  if not self.IsStopped then
    return 
  elseif startTime - tick() > 0
    break
  end
  task.wait(1)
  local totalTime = startTime - tick()
  
  local minutes = math.floor((totalTime % 3600) / 60)
  local seconds = math.floor(totalTime % 60)
 
  self.instance[self.property] = ('%02d:%02d'):format(minutes, seconds)
 end
 
 callback(...)
end
 
local new = function(instance: instance, property: string)
 return setmetatable({
  Instance = instance,
  property = property,
  Render = nil,
  IsStopped = false
 }, timer)
end
 
return {new = new}

ServerScript:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TimerService = require(...)

local GameTimerValue = ReplicatedStorage:WaitForChild("GameTimer")
local GameTimer = TimerService.new(
GameTimerValue,
"Value")

pl.PlayerAdded:Connect(function()
  if #pl:GetPlayers() > 1 then
     GameTimer:Play(60*5, function() 
       task.wait(3)
       for _, plr in pl:GetPlayers() do
         player.Team = teamw
         --code
       end
     end)
   else
     GameTimer:Stop()
     GameTimerValue.Value = "Please Wait Players..."
   end
end)

Local script

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local GameTimerValue = ReplicatedStorage:WaitForChild("GameTimer")

GameTimerValue:GetPropertyChangedSignal():Connect(function()
   print(GameTimeValue.Value)
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.