Why doesn't the tag work and doesn't give += 1 Kill?

My Icestaff script doesn’t work :frowning:

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(enemyHumanoid)
				TagHumanoid(enemyHumanoid,humanoid)
				
				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)

Script that gives the Kill in the Leaderstats!

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function()
		Player.Character.Humanoid.Died:Connect(function()
			local humanoid = Player.Character:WaitForChild("Humanoid")
			local tag = humanoid:FindFirstChild("creator")
			if tag and tag.Value then
				local PlayerName = tag.Value
				local killer = game:GetService("Players"):FindFirstChild(PlayerName.Name)
				killer.leaderstats.Kill.Value += 1
			end
		end)
	end)
end)

Script from the Classic Sword(works):

--Rescripted by Luckymaxer
--EUROCOW WAS HERE BECAUSE I MADE THE PARTICLES AND THEREFORE THIS ENTIRE SWORD PRETTY AND LOOK PRETTY WORDS AND I'D LIKE TO DEDICATE THIS TO MY FRIENDS AND HI LUCKYMAXER PLS FIX SFOTH SWORDS TY LOVE Y'ALl
--Updated for R15 avatars by StarWars
--Re-updated by TakeoHonorable

Tool = script.Parent
Handle = Tool:WaitForChild("Handle")

function Create(ty)
	return function(data)
		local obj = Instance.new(ty)
		for k, v in pairs(data) do
			if type(k) == 'number' then
				v.Parent = obj
			else
				obj[k] = v
			end
		end
		return obj
	end
end

local BaseUrl = "rbxassetid://"

Players = game:GetService("Players")
Debris = game:GetService("Debris")
RunService = game:GetService("RunService")

DamageValues = {
	BaseDamage = 5,
	SlashDamage = 10,
	LungeDamage = 30
}

--For R15 avatars
Animations = {
	R15Slash = 522635514,
	R15Lunge = 522638767
}

Damage = DamageValues.BaseDamage

Grips = {
	Up = CFrame.new(0, 0, -1.70000005, 0, 0, 1, 1, 0, 0, 0, 1, 0),
	Out = CFrame.new(0, 0, -1.70000005, 0, 1, 0, 1, -0, 0, 0, 0, -1)
}

Sounds = {
	Slash = Handle:WaitForChild("SwordSlash"),
	Lunge = Handle:WaitForChild("SwordLunge"),
	Unsheath = Handle:WaitForChild("Unsheath")
}

ToolEquipped = false

--For Omega Rainbow Katana thumbnail to display a lot of particles.
for i, v in pairs(Handle:GetChildren()) do
	if v:IsA("ParticleEmitter") then
		v.Rate = 20
	end
end

Tool.Grip = Grips.Up
Tool.Enabled = true

function IsTeamMate(Player1, Player2)
	return (Player1 and Player2 and not Player1.Neutral and not Player2.Neutral and Player1.TeamColor == Player2.TeamColor)
end

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

function Blow(Hit)
	if not Hit or not Hit.Parent or not CheckIfAlive() or not ToolEquipped then
		return
	end
	local RightArm = Character:FindFirstChild("Right Arm") or Character:FindFirstChild("RightHand")
	if not RightArm then
		return
	end
	local RightGrip = RightArm:FindFirstChild("RightGrip")
	if not RightGrip or (RightGrip.Part0 ~= Handle and RightGrip.Part1 ~= Handle) then
		return
	end
	local character = Hit.Parent
	if character == Character then
		return
	end
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	if not humanoid or humanoid.Health == 0 then
		return
	end
	local player = Players:GetPlayerFromCharacter(character)
	if player and (player == Player or IsTeamMate(Player, player)) then
		return
	end
	UntagHumanoid(humanoid)
	TagHumanoid(humanoid, Player)
	humanoid:TakeDamage(Damage)	
end


function Attack()
	Damage = DamageValues.SlashDamage
	Sounds.Slash:Play()

	if Humanoid then
		if Humanoid.RigType == Enum.HumanoidRigType.R6 then
			local Anim = Instance.new("StringValue")
			Anim.Name = "toolanim"
			Anim.Value = "Slash"
			Anim.Parent = Tool
		elseif Humanoid.RigType == Enum.HumanoidRigType.R15 then
			local Anim = Tool:FindFirstChild("R15Slash")
			if Anim then
				local Track = Humanoid:LoadAnimation(Anim)
				Track:Play(0)
			end
		end
	end	
end

function Lunge()
	Damage = DamageValues.LungeDamage

	Sounds.Lunge:Play()
	
	if Humanoid then
		if Humanoid.RigType == Enum.HumanoidRigType.R6 then
			local Anim = Instance.new("StringValue")
			Anim.Name = "toolanim"
			Anim.Value = "Lunge"
			Anim.Parent = Tool
		elseif Humanoid.RigType == Enum.HumanoidRigType.R15 then
			local Anim = Tool:FindFirstChild("R15Lunge")
			if Anim then
				local Track = Humanoid:LoadAnimation(Anim)
				Track:Play(0)
			end
		end
	end	
	--[[
	if CheckIfAlive() then
		local Force = Instance.new("BodyVelocity")
		Force.velocity = Vector3.new(0, 10, 0) 
		Force.maxForce = Vector3.new(0, 4000, 0)
		Debris:AddItem(Force, 0.4)
		Force.Parent = Torso
	end
	]]
	
	wait(0.2)
	Tool.Grip = Grips.Out
	wait(0.6)
	Tool.Grip = Grips.Up

	Damage = DamageValues.SlashDamage
end

Tool.Enabled = true
LastAttack = 0

function Activated()
	if not Tool.Enabled or not ToolEquipped or not CheckIfAlive() then
		return
	end
	Tool.Enabled = false
	local Tick = RunService.Stepped:wait()
	if (Tick - LastAttack < 0.2) then
		Lunge()
	else
		Attack()
	end
	LastAttack = Tick
	--wait(0.5)
	Damage = DamageValues.BaseDamage
	local SlashAnim = (Tool:FindFirstChild("R15Slash") or Create("Animation"){
		Name = "R15Slash",
		AnimationId = BaseUrl .. Animations.R15Slash,
		Parent = Tool
	})
	
	local LungeAnim = (Tool:FindFirstChild("R15Lunge") or Create("Animation"){
		Name = "R15Lunge",
		AnimationId = BaseUrl .. Animations.R15Lunge,
		Parent = Tool
	})
	Tool.Enabled = true
end

function CheckIfAlive()
	return (((Player and Player.Parent and Character and Character.Parent and Humanoid and Humanoid.Parent and Humanoid.Health > 0 and Torso and Torso.Parent) and true) or false)
end

function Equipped()
	Character = Tool.Parent
	Player = Players:GetPlayerFromCharacter(Character)
	Humanoid = Character:FindFirstChildOfClass("Humanoid")
	Torso = Character:FindFirstChild("Torso") or Character:FindFirstChild("HumanoidRootPart")
	if not CheckIfAlive() then
		return
	end
	ToolEquipped = true
	Sounds.Unsheath:Play()
end

function Unequipped()
	Tool.Grip = Grips.Up
	ToolEquipped = false
end

Tool.Activated:Connect(Activated)
Tool.Equipped:Connect(Equipped)
Tool.Unequipped:Connect(Unequipped)

Connection = Handle.Touched:Connect(Blow)

Error in the script that gives the Kill:

ServerScriptService.Killplayer:9: attempt to index nil with 'leaderstats'  -  Server - Killplayer:9
4 Likes

I’ve been looking at your code for a while and I can’t seem to figure out how to fix the problem… before updating the leaderstats, try printing killer, maybe that’ll give some info

3 Likes
game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function()
		Player.Character.Humanoid.Died:Connect(function()
			local humanoid = Player.Character:WaitForChild("Humanoid")
			local tag = humanoid:FindFirstChild("creator")
			if tag and tag.Value then
				local PlayerName = tag.Value
				local killer = game:GetService("Players"):FindFirstChild(PlayerName.Name)
				killer.leaderstats.Kill.Value += 1
				print(killer)
			end
		end)
	end)
end)

it doesn’t print at all!

3 Likes

You uhm, placed the print statement after… I meant before the

3 Likes

Oh sorry I did’t notice that XD

3 Likes

Sorry for the late response :frowning:

nil  -  Server - Killplayer:9
3 Likes

Let’s do some debugging in the receiving script!

Could you run this code with some prints added:

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function()
        print("char added")
		Player.Character.Humanoid.Died:Connect(function()
            print("hum death")
			local humanoid = Player.Character:WaitForChild("Humanoid")
			local tag = humanoid:FindFirstChild("creator")
			if tag and tag.Value then
                print("tag found")
				local PlayerName = tag.Value
				local killer = game:GetService("Players"):FindFirstChild(PlayerName.Name)
                print("killer found: "..killer)
				killer.leaderstats.Kill.Value += 1
                print("KillVal changed")
			end
		end)
	end)
end)

Now it would be appreciated if you could send the output of the console after running this.

3 Likes

I will do that just wait a bit!

3 Likes

In this code you’re using Debris:AddItem(Creator_Tag, 2) which destroys the value try removing that and trying again

3 Likes
   ▶ char added (x2)  -  Server - Killplayer:3
  hum death  -  Server - Killplayer:5
    tag found  -  Server - Killplayer:9
   ServerScriptService.Killplayer:12: attempt to concatenate string with nil  -  Server - Killplayer:12
  Stack Begin  -  Studio
 Script 'ServerScriptService.Killplayer', Line 12  -  Studio - Killplayer:12
 Stack End  -  Studio
 char added
3 Likes

That should not be causing the problem as only one instance gets destroyed when there are 2, meaning 1 will be left over. As seen this exact line is also used in the ClassicalSword from Roblox which works perfectly.

3 Likes

Yeah right got it, I just realized that you already do have the player instance with “tag.Value”.

This should work:

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function()
		Player.Character.Humanoid.Died:Connect(function()
			local humanoid = Player.Character:WaitForChild("Humanoid")
			local tag = humanoid:FindFirstChild("creator")
			if tag and tag.Value then
				local killer = tag.Value
				killer.leaderstats.Kill.Value += 1
			end
		end)
	end)
end)
3 Likes
leaderstats is not a valid member of Humanoid "Workspace.Player1.Humanoid" -  Server - Killplayer:8
3 Likes

Oh yeah, you are passing the humanoid of the player in the TagHumanoid function even though it should be the player instance.

Your tool script should be correct like this:

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(enemyHumanoid)
				TagHumanoid(enemyHumanoid, player)
				
				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)
3 Likes

No i don’t know if you think that because of the “Debris:AddItem(Creator_Tag, 2)” but there only gets created 1 as far as i seen so it should be the thing causing it with “debris:AddItem” it has a: object, b:time, a is the object that’s going to get destroyed and b is the time before it gets destroyed

3 Likes

Well that worked perfectly fine :smiley:

3 Likes

Oh, just read through the docs and you actually are right (I never really used Debris for myself that’s my bad) but to explain why the line is not a problem: the tag does not have to exist longer then 2 seconds because if the target hit dies, it will get the tag immediately anyways and run the code like normal, the line is just for cleanup I assume but indeed it would not be needed as there is the untagHumanoid function. Not sure why it is in ClassicalSword.

3 Likes

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