Why is this script getting this error?

Im trying to make a kill streak script, but Im having a problem were it gives me this error

  17:22:55.812  ServerScriptService.Script:17: attempt to index nil with 'Value'  -  Server - Script:17
  17:22:55.812  Stack Begin  -  Studio
  17:22:55.813  Script 'ServerScriptService.Script', Line 17  -  Studio - Script:17
  17:22:55.813  Stack End  -  Studio

is their anyway I can fix this?

local RunService = game:GetService("RunService")
local playerKills = {}

game:GetService("Players").PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		local creator = humanoid:FindFirstChild("creator")

		humanoid.Died:Connect(function()
			if creator and creator.Value then

				playerKills[creator.Value] += 3
			end
		end)
		
		while task.wait() do
			if playerKills[creator.Value] == 3 then
				print("yay")
			end
		end
	end)
end)

It is saying that on line 17, “creator” is nil, and is throwing an error when you try accessing “creator.Value”

You can avoid this with

if creator then

on the line before, to check to make sure “creator” isn’t nil.

2 Likes

You should move the playerkills[creator.Value] check into the if creator check, and also define the creator in the humanoid.Died connection.

local RunService = game:GetService("RunService")
local playerKills = {}

game:GetService("Players").PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")

		humanoid.Died:Connect(function()
			local creator = humanoid:FindFirstChild("creator")

			if creator and creator.Value then
				playerKills[creator.Value] += 3

				if playerKills[creator.Value] == 3 then
					print("yay")
				end
			end
		end)
	end)
end)

Now the function doesn’t fire at all.

This means their isn’t any creator value in the humanoid, so should I put in a string value inside of the humanoid.

Yeah, I think that this is how older leaderboard and tools worked. They’d have a ObjectValue named “creator” that they would put into projectiles. The Value was the firing player, and when the projectile hurt a victim player, the projectile would put that creator tag into the humanoid so that the humanoid knew who hurt it.

1 Like
if playerKills[creator.Value] == 3 then

You forgot the if creator and {expression list} then part. creator in this case is being recognised as ‘nil’.