I have been making a game with my friend and I came across a problem. I scripted a cash per kill for an npc based on a tutorial
This is the code:
local Humanoid = script.Parent.Humanoid
function Dead()
local tag = Humanoid:FindFirstChild("creator")
if tag ~= nil then
if tag.Value ~= nil then
local leaderstats = tag.Value:FindFirstChild("leaderstats")
if leaderstats ~= nil then
leaderstats.Cells.Value = leaderstats.Cells.Value + 50
end
end
end
end
It would not give me cash on kill.
Here is a video on my problem:
I’ve tried looking at multiple tutorials, but each end up with the same result, failure.
Maybe this will work, haven’t tested it so let me know if it works.
local Humanoid = script.Parent.Humanoid
function TagHumanoid(Hum, Attacker)
if not Hum:FindFirstChild("Creator_Tag") then
local CreatorTag = Instance.new("ObjectValue")
CreatorTag.Name = "Creator_Tag"
CreatorTag.Value = Attacker
CreatorTag.Parent = Hum
game.Debris:AddItem(CreatorTag, 2)
elseif Hum:FindFirstChild("Creator_Tag") then
local CreatorTag = Hum:FindFirstChild("Creator_Tag")
CreatorTag.Value = Attacker
end
end
function Died()
local Tag = Humanoid:FindFirstChild("Creator_Tag")
if Tag ~= nil and Tag.Value ~= nil then
local PlayerOfAttacker = game.Players:GetPlayerFromCharacter(Tag)
if PlayerOfAttacker:IsA("Player") then
local Leaderstats = PlayerOfAttacker:FindFirstChild("leaderstats")
if Leaderstats then
Leaderstats.Cells.Value = Leaderstats.Cells.Value + 50
end
end
end
end
local sword = script.Parent
local canDMG = false
local function onTouch(otherPart)
local humanoid = otherPart.Parent:FindFirstChild("Humanoid")
if not humanoid then
return
end
if humanoid.Parent ~= sword.Parent and canDMG then
humanoid:TakeDamage(10)
else
return
end
canDMG = false
end
local function slash()
local str = Instance.new("StringValue")
str.Name = "toolanim"
str.Value = "Slash"
str.Parent = sword
canDMG = true
end
sword.Activated:Connect(slash)
sword.Handle.Touched:Connect(onTouch)