Unable to cast value to Object, how can I fix this

I’ve been trying for the past few days to fix this bug but nothing really seems to work,

function endRound(winner)
	print("End of round")
	print(winner.Name .. " won the game!")
	
	for i, plr in pairs(game.Players:GetChildren()) do

		print("Updating")
		updateKills:InvokeClient(plr, redPlayerKills, bluePlayerKills)
		winGame:InvokeClient(winner.Name)
	end
	

	--get rid of  all connections
	
		warn(101)
	for i, plr in pairs(game.Players:GetChildren()) do
		clearPlayers:InvokeClient(plr)
			warn(202)
		plr.RespawnLocation = workspace.Start.SpawnLocation

		if plr.Character:FindFirstChild("HumanoidRootPart") then
			plr.Character.HumanoidRootPart.Position =  workspace.Start.SpawnLocation.Position  + Vector3.new(0,5,0)

		end
		plr.Team = game.Teams.Waiting
	end
	for i, connect  in pairs (connections) do
		connect:Disconnect()
	end
	wait(15)

print("Starting new round")
	redPlayerKills = {}
	bluePlayerKills = {}
	--waiting till next round
	
	roundHandler()
	warn(303)
	
end

local maxKills = 41

local lastPlayer = nil
game.ReplicatedStorage.KillPlayer.OnInvoke = function<string>(player1 : string, playerKilled1 : string)
	warn("test")
	local player = game.Players:FindFirstChild(player1)
	local playerKilled = game.Players:FindFirstChild(playerKilled1)
	if lastPlayer ~= player then
		lastPlayer = player
		
		warn(player.Name .. " got a kill")
		
		for i, plrStats in pairs(redPlayerKills) do
			print(plrStats)
			if plrStats[1] == player then
				plrStats[2] += 1 

				print(plrStats[2])

				if  plrStats[2] >= maxKills then 

					endRound(player)

				end
			end


		end

		for i, plrStats in pairs(bluePlayerKills) do
			if plrStats[1] == player then
				plrStats[2] += 1 

				print(plrStats[2])

				if  plrStats[2] >= maxKills then 

					endRound(player)

				end
			end

		end
		warn("passed")

		for i, plr in pairs(game.Players:GetChildren()) do

			print("Updating1")
			updateKills:InvokeClient(plr, redPlayerKills, bluePlayerKills)
		end

		
		coroutine.wrap(function()

			wait(.25)
			lastPlayer = nil
		end)()
	end
	print("Killed")
	game.ReplicatedStorage.KillFeed:FireAllClients(player.Name, playerKilled.Name)
end

The 41th time it is invoked with the same parameters, it just throws an error saying “Unable to cast value to Object”, when invoked it is always given 2 strings, the killer’s name and the victim’s name.
It throws the error after printing “Updating” once, therefore it stops after one iteration in the endRound function. Anything that can help fix this bug is appreciated!

1 Like

Is your enter key sticky or something? Why the HELL do you need this much blank space…
what line is the error occuring

You forgot that when a remote is invoked, the first parameter is always a player object, not a string.

It’s a bindable function, it is not fired by a client! and first tried with the parameters as “Player” instance, didn’t work!

The line that errors is the line that invokes it from a different script

updateKills:InvokeClient(plr, redPlayerKills, bluePlayerKills)

So this line is the one causing the error?

Here !

Something is erroring somewhere during your OnInvoke callback, specifically this line:

winGame:InvokeClient(winner.Name)

Change that to winner. That error shows up if you are passing an invalid argument that expects an Instance.

2 Likes

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