Why doesn't the creator tag work?

weapon script(on server):

local tween = game:GetService("TweenService")
local casting = false
local Debris = game:GetService("Debris")

function TagHumanoid(humanoid, player)
	local Creator_Tag = Instance.new("ObjectValue")
	Creator_Tag.Name = "creator"
	Creator_Tag.Value = player
	Debris:AddItem(Creator_Tag, 2)
	Creator_Tag.Parent = humanoid
end

function UntagHumanoid(humanoid)
	for i, v in pairs(humanoid:GetChildren()) do
		if v:IsA("ObjectValue") and v.Name == "creator" then
			v:Destroy()
		end
	end
end

script.Parent.RemoteEvent.OnServerEvent:Connect(function(player)
	if casting == true then return end
	casting = true
	
	local iceSpikes = game.ReplicatedStorage.IceSpikes:Clone()
	local attackAnim = player.Character.Humanoid:LoadAnimation(script.Parent.Attack)
	local IceSound = script.Parent["Ice 2 SFX"]
	local TrailElecriticy = game.ServerStorage.TrailElectricity:Clone()
	local TrailElecriticyBlur = game.ServerStorage.TrailElectricityBlur:Clone()
	IceSound:Play()
	attackAnim:Play()
	
	iceSpikes:SetPrimaryPartCFrame(player.Character.HumanoidRootPart.CFrame)
	iceSpikes.Parent = workspace
	local endPosition = {Position = {}, Orientation = {}}
	for i, v in pairs(iceSpikes.Spikes:GetChildren()) do
		endPosition["Position"][i] = v.Position
		endPosition["Orientation"][i] = v.Orientation
		v.CFrame = CFrame.new(iceSpikes.Origin.Position)
	end
	
	wait(0.4)
	for i,v in pairs(iceSpikes.Spikes:GetChildren()) do
		v.Orientation = endPosition["Orientation"][i]
		tween:Create(v, TweenInfo.new(0.4),{Position = endPosition["Position"][i]}):Play()
	end
	
	for i,v in pairs(iceSpikes.Spikes:GetChildren()) do
		v.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") and not hit.Parent:FindFirstChild("AlreadyHit") and not hit:IsDescendantOf(player.Character) then
				local humanoid = player.Character:FindFirstChild("Humanoid")
				local enemyHumanoid = hit.Parent:FindFirstChild("Humanoid")
				local enemy = hit.Parent
				local alreadyHit = Instance.new("BoolValue")
				alreadyHit.Parent = hit.Parent
				alreadyHit.Name = "AlreadyHit"
				
				UntagHumanoid(humanoid)
				TagHumanoid(humanoid,enemyHumanoid)
				
				enemyHumanoid:TakeDamage(25)
				game.ReplicatedStorage.Freeze:Fire(enemyHumanoid,enemy,TrailElecriticy,TrailElecriticyBlur,alreadyHit)
			end
		end)
	end
	
	wait(1.2)
	iceSpikes:Destroy()
	wait(5)
	casting = false
end)

leaderstats(on server):

local dataStore = game:GetService("DataStoreService")
local KillStore = dataStore:GetDataStore("KillStore")
game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local kill = Instance.new("NumberValue")
	kill.Name = "Kill"
	kill.Value = 0
	kill.Parent = leaderstats
	
	player.CharacterAdded:Connect(function(Character)
		Character.Humanoid.Died:Connect(function(Died)
			local creator = Character.Humanoid:FindFirstChild("creator")
			local leaderstats = creator.Value:FindFirstChild("leaderstats")
			if creator ~= nil and creator.Value ~= nil then
				leaderstats.Kill.Value = leaderstats.Kill.Value + 1
			end
		end)
	end)
	
	local data
	local success,errormessage = pcall(function()
		data = KillStore:GetAsync(player.UserId.."-kill")
	end)
	
	if success then
		kill.Value = data
	else
		print("There was an error while getting your data")
		warn(errormessage)
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success,errormessage = pcall(function()
		KillStore:SetAsync(player.UserId.."-kill",player.leaderstats.Kill.Value)
	end)
	if success then
		
		print("player Data successfully saved!")
	else
		print("There was an error saving data")
		warn(errormessage)
	end
end)

error(on server):

ServerScriptService.leaderstats:16: attempt to index nil with 'Value'
2 Likes

The error is likely related to the creator tag variable being nil when you try to access its value property

try this updated code:

function TagHumanoid(humanoid, player)
    local Creator_Tag = humanoid:FindFirstChild("creator")
    
    if not Creator_Tag then
        Creator_Tag = Instance.new("ObjectValue")
        Creator_Tag.Name = "creator"
        Debris:AddItem(Creator_Tag, 2)
        Creator_Tag.Parent = humanoid
    end
    
    Creator_Tag.Value = player
end

function UntagHumanoid(humanoid)
    local creatorTag = humanoid:FindFirstChild("creator")
    
    if creatorTag then
        creatorTag:Destroy()
    end
end
1 Like

Same error appears, it doesn’t work!

1 Like

The script is telling you that there isn’t a creator tag in the humanoid. You need to check if the creator tag exists before accessing leaderstats:

Character.Humanoid.Died:Connect(function(Died)
			local creator = Character.Humanoid:FindFirstChild("creator")
			if creator ~= nil and creator.Value ~= nil then
			local leaderstats = creator.Value:FindFirstChild("leaderstats") — access leaderstats only after a tag is found
				leaderstats.Kill.Value = leaderstats.Kill.Value + 1
			end
		end)
1 Like

No error still doesn’t work, do I need to use the fix by veysuki?!

1 Like

I have no idea what the fix you mentioned is, but yeah CharacterAdded is the problem and it is running after a character has spawned. You need a check if the character has already spawned to run the event:

local function CharacterAdded(player) — function for characterAdded
local Character = player.Character

Character.Humanoid.Died:Connect(function(Died)
			local creator = Character.Humanoid:FindFirstChild("creator")
			if creator ~= nil and creator.Value ~= nil then
			local leaderstats = creator.Value:FindFirstChild("leaderstats") — access leaderstats only after a tag is found
				leaderstats.Kill.Value = leaderstats.Kill.Value + 1
			end
		end)
end

game.Players.PlayerAdded:Connect(function(player)
— create leaderstats
if player.Character then CharacterAdded(player) — run CharacterAdded if character already existed
end
player.CharacterAdded:Connect(function(Character)
CharacterAdded(player)
end)
— rest of code
end)

Sorry but this code is very bad formated!

Sorry I am on phone so I can’t format it but doesn’t putting the script into Script Editor automatically formats the code? Additionally you can reformat the script by opening Script Editor and going to Script → Format → Format Document