I’m a very new scripter so do not judge me for that
My goal: There is a killer npc on my game and I want that player get +100 money when player killed the NPC.
game.Players.PlayerAdded:connect(function(p)
local stats = Instance.new("IntValue")
stats.Name = "leaderstats"
stats.Parent = p
local Money = Instance.new("IntValue")
Money.Name = "Money"
Money.Value = 0
Money.Parent = stats
end)
another script
local function onNPCDied(diedCharacter)
-- Check if the NPC has a humanoid idk if I make this wrong
local humanoid = diedCharacter:FindFirstChild("Humanoid")
if humanoid then
-- Add money to the player's account
local player = game.Players:GetPlayerFromCharacter(diedCharacter)
if player then
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local money = leaderstats:FindFirstChild("Money")
if money then
money.Value = money.Value + 100
end
end
end
end
end
-- Find all NPCs in the game
local npcs = game.Workspace:FindFirstChild("NPCs")
if npcs then
npcs.ChildAdded:Connect(function(npc)
-- Connect the Died event for each NPC
if npc:FindFirstChild("Humanoid") then
npc.Humanoid.Died:Connect(function()
onNPCDied(npc)
end)
end
end)
end
Add prints to see what is actually going on, it’s hard to tell for a person looking in. Does the npc.Humanoid.Died event actually get fired? You’ll need to put a print in there to see.
i see the the topic has been marked solved but it doesnt seem to me thus so imma go ahead and toss in my 2 quarters worth of opinion
uhhh, isnt diedCharacter the npc which the player killed? unless im understanding smth wrong, i dont think this will return the player, which may be why nothings happening
npc’s name is NPC in workspace.I got some help from an other lua student. Idk if he made it a variable as DiedCharacter. I’m confused, idk where is wrong in second script. Or can you make an example script?
unfortunately, there isnt much i could do for you without further context because your script does not show me which player killed the NPC.
When you called game.Players:GetPlayerFromCharacter(diedCharacter), you passed as the parameter the body of the dead NPC, which was passed into the function as the parameter diedCharacter, rather than the player who killed it (which I dont know how to get solely from this script alone). since there is no player for said NPC, it returns nil and the the rest of your code does not run.