Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Player killed and NPC killed not giving money correctly?

Asked by 2 years ago

When I kill the NPC in studio, the console always prints "nil" when I try to get a creator tag. I am using my cursor to click a player/NPC and do damage. What do I have to do so that the server can see who killed another player? If I can get the tag then I should be able to give the cash but nothing I've tried has worked so far.

local Humanoid = script.Parent.Humanoid
function Dead()
    local tag = Humanoid:FindFirstChild("creator")
    print(tag)
    if tag ~= nil then
        if tag.Value ~= nil then
            local leaderstats = tag.Value:FindFirstChild("leaderstats")
            if leaderstats ~= nil then
                leaderstats.Cash.Value += 10
                wait()
            end
        end
    end
end
Humanoid.Died:Connect(Dead)

this is the script inside of the NPC. When I kill it, the console prints "nil" instead of the tag.

local function onPlayerAdded(player)
    local playerKey = "Player_" .. player.UserId
    wait()
    print("User ".. player.UserId.. " Joined")

    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local Cash = Instance.new("IntValue")
    Cash.Name = "Cash"
    Cash.Parent = leaderstats

    local myCash
    local success, err = pcall(function()
          myCash = CashStore:GetAsync(playerKey) or STARTING_CASH
    end)
    if success then
        Cash.Value = myCash
        print("loading "..myCash)
    else
        print("failed to retrieve data")
        warn(err)
    end

player.CharacterAdded:Connect(function(character)
    character:WaitForChild("Humanoid").Died:Connect(function(killed)
        local tag = character.Humanoid:FindFirstChild("creator")
        if tag and tag.Value then
            local stats = tag.Value:WaitForChild("leaderstats")
            wait()
            stats["Cash"].Value = stats["Cash"].Value + 15
            print(Cash.Value)
        end
    end)
end)
end
game.Players.PlayerAdded:Connect(onPlayerAdded)

This is the main script. In this one, you can see I have tried a different approach to printing and changing the cash value but nothing is working. Why is the creator tag not working for me?

Answer this question