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

Have Kill For Money Script, But Gives To Wrong Player?

Asked by 6 years ago

I really want a script so that if I was to kill you I would get money, here's the script I have so far, but the thing is in this script if I kill you, you get the money: game.Players.PlayerAdded:connect(function(player) local folder = Instance.new("Folder",player) folder.Name = "leaderstats" local currency1 = Instance.new("IntValue",folder) currency1.Name = "Cash" player.CharacterAdded:connect(function(character) character:WaitForChild("Humanoid").Died:connect(function() local tag = character.Humanoid:FindFirstChild("creator") if tag ~= nil then if tag.Value ~= nil then currency1.Value = currency1.Value + 10 --This is the reward after the player died. end end end) end) end)I've looked around and can't seem to find the answer. Can you please help me? Thank you for your time.

2 answers

Log in to vote
1
Answered by 6 years ago

Your code is almost right. Once you find the creator tag, it will hold the value of the person that killed us, so we need to find their leaderstats.Cash and add to it.

game.Players.PlayerAdded:connect(function(player)
    local folder = Instance.new("Folder",player)
    folder.Name = "leaderstats"
    local currency1 = Instance.new("IntValue",folder)
    currency1.Name = "Cash"

    player.CharacterAdded:connect(function(character)
        character:WaitForChild("Humanoid").Died:connect(function()
            local tag = character.Humanoid:FindFirstChild("creator")
            if tag ~= nil then
                if tag.Value ~= nil then
                    -- Here tag.Value is the player who killed us, so find their leaderstats
                    local killersLeaderstats = tag.Value:FindFirstChild("leaderstats")
                    if killersLeaderstats then
                        -- Award the cash to the player that killed us
                        killersLeaderstats.Cash.Value = killersLeaderstats.Cash.Value + 10
                    end
                end
            end
        end)
    end)
end)
0
thanks so much man, much appreciated! FlippinAwesomeCrew 62 — 6y
Ad
Log in to vote
0
Answered by
mattscy 3725 Moderation Voter Community Moderator
6 years ago

Please use a code block instead of inline code.

As for the script, you are giving the player who died the coins instead of the player who killed. You should be getting the player who killed through the value of the creator IntValue inserted into the Humanoid:

game.Players.PlayerAdded:Connect(function(plr)
    local stats = Instance.new("Model")
    stats.Name = "leaderstats"
    stats.Parent = plr
    local currency1 = Instance.new("IntValue")
    currency1.Name = "Cash"
    currency1.Parent = stats
    plr.CharacterAdded:Connect(function(char)
        char:WaitForChild("Humanoid").Died:Connect(function()
            local tag = char.Humanoid:FindFirstChild("creator")
            if tag and tag.Value and then
                plrWhoKilled = tag.Value
                if plrWhoKilled:FindFirstChild("leaderstats") and plrWhoKilled.leaderstats:FindFirstChild("Cash") then
                    plrWhoKilled.leaderstats.Cash.Value = plrWhoKilled.leaderstats.Cash.Value + 10
                end
            end
        end)
    end)    
end)

Keep in mind this will only work with weapons that insert this creator value into the humanoid on kill.

0
thanks for the tip on how to present my code as for the code itself, much appreciated! FlippinAwesomeCrew 62 — 6y
0
if my Weapon doesn't have the "Humanoid" where should i put it in? i tried to put it in but the weapon killed it self :D Wellerkinds 0 — 5y
0
That script should be somewhere like ServerScriptService mattscy 3725 — 5y

Answer this question