Attempt to call a nil value

Hi roblox!
im following along @Alvin_Blox’s Hide and seek game, however ive come across some errors:

ServerScriptService.MainScript:62: attempt to call a nil value

Heres the main parts of this error:

	for i = 300,0,-1 do
		status.Value = "Game In Progress"
		timer.Value = toMS(i)
		wait(0.01)
		

And

local function toMS(s)
return ("%02i:%02i"):format(s/60%60, s%60)

can anyone help me fix this?

Can you please post the full script and also highlight the line where it throws an error?

1 Like

sure here it is

--Services--
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local serverStorage = game:GetService("ServerStorage")

local status = ReplicatedStorage.Values.Status

local map = game.Workspace.TempMap

local timer = game.ReplicatedStorage.Values.Timer

--Config
local PlayersToStart = 1

local function ChooseSeeker(availablePlayers,position,spawns)
	return availablePlayers[math.random(1, #availablePlayers)]
end

local function TeleportPlayers(availablePlayers,spawns)
	for _, plr in pairs(availablePlayers) do
		if plr.Character then
			if plr.Character:FindFirstChild("HumanoidRootPart") then
				plr.Character.HumanoidRootPart.CFrame = spawns[math.random(1, #spawns)].CFrame + Vector3.new(0,5,0)
				
				local function toMS(s)
					return ("%02i:%02i"):format(s/60%60, s%60)
				end
			end
		end
	end
end

while wait(1) do
	
	repeat
		
		print("not enough players in the game")
		status.Value = PlayersToStart.." Players Needed To Start (1/"..PlayersToStart..")"
		
		
	until #game.Players:GetPlayers() >= PlayersToStart
	
	for i = 5,0.-1 do
		status.Value = "Next Round Starts In"..i.."Seconds"
		wait(0.1)
		
		
	end
	
	local contestants = game.Players:GetPlayers()
	
	
	local seeker = ChooseSeeker(contestants)
	
	status.Value = seeker.Name.."Is the seeker!"
	
	wait(3)
	
	TeleportPlayers(contestants,map.Spawns:GetChildren())
	
	for i = 300,0,-1 do
		status.Value = "Game In Progress"
		timer.Value = toMS{i}
		wait(0.01)
		
	end
end

Error the line is on is:
timer.Value = toMS(i)

Yeah, the function you were trying to call was a local function in a different scope.

Try this code:

--Services--
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local serverStorage = game:GetService("ServerStorage")

local status = ReplicatedStorage.Values.Status

local map = game.Workspace.TempMap

local timer = game.ReplicatedStorage.Values.Timer

--Config
local PlayersToStart = 1

local function ChooseSeeker(availablePlayers,position,spawns)
	return availablePlayers[math.random(1, #availablePlayers)]
end

local function toMS(s)
    return ("%02i:%02i"):format(s/60%60, s%60)
end

local function TeleportPlayers(availablePlayers,spawns)
	for _, plr in pairs(availablePlayers) do
		if plr.Character then
			if plr.Character:FindFirstChild("HumanoidRootPart") then
				plr.Character.HumanoidRootPart.CFrame = spawns[math.random(1, #spawns)].CFrame + Vector3.new(0,5,0)
			end
		end
	end
end

while wait(1) do
	
	repeat
		
		print("not enough players in the game")
		status.Value = PlayersToStart.." Players Needed To Start (1/"..PlayersToStart..")"
		
		
	until #game.Players:GetPlayers() >= PlayersToStart
	
	for i = 5,0.-1 do
		status.Value = "Next Round Starts In"..i.."Seconds"
		wait(0.1)
		
		
	end
	
	local contestants = game.Players:GetPlayers()
	
	
	local seeker = ChooseSeeker(contestants)
	
	status.Value = seeker.Name.."Is the seeker!"
	
	wait(3)
	
	TeleportPlayers(contestants,map.Spawns:GetChildren())
	
	for i = 300,0,-1 do
		status.Value = "Game In Progress"
		timer.Value = toMS{i}
		wait(0.01)
		
	end
end
3 Likes

I think it’s because function toMS() is local

1 Like
return ("%02i:%02i"):format(s/60%60, s%60)

ServerScriptService.MainScript:19: attempt to perform arithmetic (div) on table and number

time.Value = toMS(i)
Try this.

2 Likes

On line 62, replace timer.Value = toMS{i} with timer.Value = toMS(i)

1 Like

You used curly braces instead of round brackets.

Replace:

timer.Value = toMS{i}

With:

timer.Value = toMS(i)
1 Like

Probably a script error in a different script but it works fine the first time but next time this happens:

robloxapp-20210831-2202242.wmv (2.1 MB)

Btw heres the other script

--Services--
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local serverStorage = game:GetService("ServerStorage")

local status = ReplicatedStorage.Values.Status

local timer = ReplicatedStorage.Values.Timer

local playersleft = ReplicatedStorage.Values.PlayersLeft

local HUD = script.Parent
local StatusFrame = HUD:WaitForChild("Timer")
local PlayersLeftGui = HUD:WaitForChild("PlayersLeft")

StatusFrame.TimeLeft.Text = status.Value

status:GetPropertyChangedSignal("Value"):Connect(function()
	StatusFrame.TimeLeft.Text = status.Value
	StatusFrame.Timer.Text = timer.Value
	PlayersLeftGui.PlayersLeft.Text = playersleft.Value
end)

timer:GetPropertyChangedSignal("Value"):Connect(function()
	StatusFrame.TimeLeft.Text = timer.Value
	
end)

playersleft:GetPropertyChangedSignal("Value"):Connect(function()
	PlayersLeftGui.PlayersLeft.Text = playersleft.Value
	
end)