Unable to find "Attempt to call a nil value"

  1. What do you want to achieve?
    A capture zone

  2. What is the issue?
    image
    When the palyer enters the zone, it starts a loop to change color etc… and prints (“Alpha”), every 2 seconds.
    After 6 seconds the clients starts making errors, even if everything is working fine.
    It will keep doing the errors until it has around the same amount as the printed Alphas

  3. What solutions have you tried so far?
    Tried to put print statements all over in the local script, but nothing was printed when the errors occured.
    and only alpha is printed around the time when the error occours

Local Script:

local currentTween = nil

Capturing.OnClientEvent:Connect(function(TF:boolean)
	local Capture = wfc(HUDContainer, "Capturing")
	local tweenInfo = TweenInfo.new(15, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
	Capture.Visible = TF

	if TF then
		Capture.Bar.Size = UDim2.new(0, 0, Capture.Bar.Size.Y.Scale, Capture.Bar.Size.Y.Offset)

		if currentTween then
			currentTween:Cancel()
			currentTween = nil
		end

		currentTween = TweenService:Create(Capture.Bar, tweenInfo, {Size = UDim2.new(1, 0, Capture.Bar.Size.Y.Scale, Capture.Bar.Size.Y.Offset)})
		currentTween:Play()

		currentTween.Completed:Connect(function()
			Capture.Visible = false
			if currentTween then
				currentTween = nil
			end
		end)

		local function updateText()
			while Capture.Visible do
				for i = 0, 3 do
					Capture.TextLabel.Text = "Capturing" .. string.rep(".", i)
					wait(0.5)
				end
			end
		end

		spawn(updateText)
	else
		if currentTween then
			currentTween:Cancel()
			currentTween = nil
		end
		Capture.Visible = false
	end
end)

Server Script

local function startColorLoop(oldColor, teamColor, player)
	local originalColor = oldColor
	local loopStartTime = tick()
	local Capturing = true
	local iterationCounter = 0

	while tick() - loopStartTime < 15 and Capturing do
		local Array = zone:getPlayers()
		if #Array <= 0 then
			Capturing = false
			CaptureRE:FireClient(player, false)
			print("Player Left Zone")
			break
		end

		for _, v in pairs(Array) do
			if v.Team.TeamColor ~= teamColor then
				Capturing = false
				print("Enemy detected")
				CaptureRE:FireClient(player, false)
				break
			end
		end

		if not Capturing then
			break
		end

		tm.Value = teamColor
		updatecolor()
		wait(0.5)
		tm.Value = originalColor
		updatecolor()
		wait(0.5)
		iterationCounter = iterationCounter + 1

		if iterationCounter == 2 then
			print("alpha")
			UpdateDataStore:Fire(player, flag, "Capturing")
			iterationCounter = 0
		end
	end

	if Capturing then
		tm.Value = teamColor
		updatecolor()
		UpdateDataStore:Fire(player, flag, "Captured")
	else
		tm.Value = originalColor
		updatecolor()
	end
end

zone.playerEntered:Connect(function(player)
	if tm.Value ~= player.TeamColor then
		CaptureRE:FireClient(player, true)
		coroutine.wrap(startColorLoop)(tm.Value, player.TeamColor, player)
	end
end)

I have absolutely no idea what causes it

image
Tried to print alot again, but at the end it again went on without printing anything other than the error

Where do you define “updatecolor”? I also recommend switching away from coroutines at least until you fix the error as they will hinder debug information (maybe use task.spawn)?

I actually tried to find the error somewhere else, atleast I think so, I tried to comment out the “UpdateDataStoreRE” and I didn’t got any errors, but I’m unsure why it’s caused by it:
SO I guess the error comes from somewhere when I pass over data from my Datastore script back to the client

DataStore:

local function UpdateValues(killer, target, Type:string)
	local function LevelUp()
		setPlayerData(killer, "Xp", 0)
		incrementPlayerData(killer, "Level", 1)
		GUIRE:FireClient(killer,"LevelUp", getPlayerData(killer, "Level"))
	end
	local currentXp = getPlayerData(killer, "Xp")
	local currentLevel = getPlayerData(killer, "Level")
	if Type == "Kill" then
		-- Increment Deaths/Kills
		incrementPlayerData(killer, "Kills", 1)
		incrementPlayerData(target, "Deaths", 1)
		-- Increment XP
		if currentXp + XpAwards.KilledXP >= currentLevel * 1000 then
			LevelUp()
		else
			incrementPlayerData(killer, "Xp", XpAwards.KilledXP)
		end
		returnData(target, killer, target, Type)
		returnData(killer, killer, target, Type)

	elseif Type == "Captured" then
		if currentXp + XpAwards.CapturedXP >= currentLevel * 1000 then
			LevelUp()
		else
			incrementPlayerData(killer, "Xp", XpAwards.CapturedXP)
		end
		returnData(killer, killer, target, Type)

	elseif Type == "Capturing" then
		if currentXp + XpAwards.CapturingXP >= currentLevel * 1000 then
			LevelUp()
		else
			incrementPlayerData(killer, "Xp", XpAwards.CapturingXP)
		end
		returnData(killer, killer, target, Type)
	end
end

-- Update DataStore event
UpdateDataStore.Event:Connect(function(killer, target, Type:string)
	UpdateValues(killer, target, Type)
end)

Local Script:

TrackDataRE.OnClientEvent:Connect(function(Kills, Deaths, Level, Xp, AddXp, Killer, Target, Type)
			if Type then
				XpAdded.Volume=0.01
				XpAdded:Play()
			end
			print(1)
			if Type=="Kill" and Target and Target:IsA("Player") and Target~= player then
				local Template="Killed:" ..tostring(Target.Name)
				local spawnedGui = SpawnKillGui(AddXp, Template)
				spawn(spawnedGui)
			elseif Type=="Captured" then
				local Template="Captured: " ..tostring(Target.Name)
				local spawnedGui = SpawnKillGui(AddXp, Template)
				spawn(spawnedGui)
			elseif Type=="Capturing" then
				local Template="Capturing"
				local spawnedGui = SpawnKillGui(AddXp, Template)
				spawn(spawnedGui)
				print(2)
			end
			local KD
			if Kills == 0 or Deaths == 0 then
				KD=0
			elseif Kills > 0 and Deaths == 0 then
				KD=Kills
			elseif Kills == 0 and Deaths > 0 then
				KD=0
			elseif Kills>0 and Deaths>0 then
				KD=Kills/Deaths
			end
			print(3)
			PlayerKills.Text = "Kills: " .. Kills
			PlayerDeaths.Text = "Deaths: " .. Deaths
			PlayerKD.Text = "K/D: " .. math.floor(KD*100)/100
			PlayerRank.Text= "Rank: " .. Ranks.Rank(Level)
			
			PlayerLevel.Text = "Level: " .. Level
			PlayerXpText.Text = "Xp: " .. Xp .. "/" .. Level*1000
			local size = math.clamp(Xp / (Level*1000), 0, 1)
			PlayerXP.Size = UDim2.new(size, 0, 1, 0)
			print(4)
		end)

Probably its cause of the function “updatecolor” you are trying to call. In the error message it says attempt to call a nil value. Either the function you are trying to call isn’t defined or you create a variable named as same as the function which tricks luau thinking you are trying to call the value instead of the function.

I would think it’s on the server side because the error information is showing to be limited, so it’s probably something originating from the coroutine. It would of course help to replace the coroutine with task or something else as to improve error information. Anyway I can’t actually see most of your function definitions so it could really be any of those. Just make sure your functions are defined correctly and there are no typos.

incrementPlayerData
returnData
getPlayerData
setPlayerData

Yes, I have already replaced the coroutine with task.spawn, I am to 100% sure that I have every Variable, function right.

THis would be the returnData function which passes over the current data back to the Client

local function returnData(player, killer, target, Type:string)
	local userId = player.UserId
	local data = playerData[userId]
	if data then
		local kills = data.Kills
		local deaths = data.Deaths
		local level = data.Level
		local xp = data.Xp
		local addXp
		if Type == "Kill" then
			addXp = XpAwards.KilledXP
		elseif Type == "Captured" then
			addXp = XpAwards.CapturedXP
		elseif Type == "Capturing" then
			addXp = XpAwards.CapturingXP
		end
		--TrackDataRE:FireClient(player, kills, deaths, level, xp, addXp, killer, target, Type)
	else
		warn("Error when returning Data")
		return nil
	end
end

I did comment out “TrackDataRE” wich is supposed to pass back the values, when I did that the error stopped occourcing

Hm, if you are sure every function is defined correctly I’m not sure but I guess we know its on the client now if commenting that fixes it. Maybe pcall the body of the OnClientEvent function and print the error, hopefully it contains error information like line numbers.

1 Like

I have commented everything out until I found the function that might actually cause it.
I had a function in the local script which tweened a textlabel.
I has something this to spawn the gui:

			if Type=="Kill" and Target and Target:IsA("Player") and Target~= player then
				local Template="Killed:" ..tostring(Target.Name)
				local spawnedGui = SpawnKillGui(AddXp, Template)
				spawn(spawnedGui)
			elseif Type=="Captured" then
				local Template="Captured: " ..tostring(Target.Name)
				local spawnedGui = SpawnKillGui(AddXp, Template)
				spawn(spawnedGui)
			elseif Type=="Capturing" then
				local Template="Capturing"
				local spawnedGui = SpawnKillGui(AddXp, Template)
				spawn(spawnedGui)
				print(2)
			end

The thing everything worked fine as intended,
and the error disappeard when i called the function like that:

			if Type=="Kill" and Target and Target:IsA("Player") and Target~= player then
				local Template="Killed:" ..tostring(Target.Name)
				task.spawn(SpawnKillGui, AddXp, Template)
			elseif Type=="Captured" then
				local Template="Captured: " ..tostring(Target.Name)
				task.spawn(SpawnKillGui, AddXp, Template)
			elseif Type=="Capturing" then
				local Template="Capturing"
				task.spawn(SpawnKillGui, AddXp, Template)
			end

Thanks for the help

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