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

Leaderstats script works only once on spawn, and doesnt work thereafter. Whats wrong?

Asked by 2 years ago

This script will only work once after the player dies or gets a kill after they spawn in. Thereafter it will not work. Why is it only registering once??

local DataStoreService = game:GetService("DataStoreService")
local DataStore1 = DataStoreService:GetDataStore("DataStore1")
local DataStore2 = DataStoreService:GetDataStore("DataStore2")
local Players = game:GetService("Players")
local work = game:GetService("Workspace")

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

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

    local overallkills = Instance.new("IntValue")
    overallkills.Name = "overallkills"
    overallkills.Parent = kills

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

    local overalldeaths = Instance.new("IntValue")
    overalldeaths.Name = "overalldeaths"
    overalldeaths.Parent = deaths



    local Statdata = player.PlayerGui:WaitForChild("Data")
    print("found gui")
    local ATK = Statdata.ATK
    local ATD = Statdata.ATD
    local ATKD = Statdata.ATKD
    local CK = Statdata.CK
    local CD = Statdata.CD
    local CKD = Statdata.CKD
    local char = work:FindFirstChild(player.Name)
    local humanoid = char:FindFirstChild("Humanoid")

        humanoid.Died:Connect(function(died)
            print(player.Name.."has died")
            deaths.Value = deaths.Value + 1
            overalldeaths.Value = overalldeaths.Value + 1
            ATD.Value = ATD.Value + 1
            CD.Value = CD.Value + 1
            local playerUserId = "Player_"..player.UserId
            local Data2 = overalldeaths.Value
            local success,errormessage = pcall(function()
                DataStore2:SetAsync(playerUserId, Data2)
            end)
            if success then
                print("Data saved after death")
            else
                print("Error saving data on death")
                warn(errormessage)
            end
            local tag = humanoid:FindFirstChild("creator")
            local killername = tag.Value
            local killer = Players:FindFirstChild(killername)
            if tag and killer then
                print(killername)
                killer.leaderstats.Kills.Value = killer.leaderstats.Kills.Value + 1
                killer.leaderstats.Kills.overallkills.Value = killer.leaderstats.Kills.overallkills.Value + 1
                killer.PlayerGui.Data.ATK.Value = killer.PlayerGui.Data.ATK.Value + 1
                killer.PlayerGui.Data.CK.Value = killer.PlayerGui.Data.CK.Value + 1
                local playerUserId2 = "Player_"..killer.UserId
                local Data1 = killer.leaderstats.Kills.overallkills.Value
                local success1, errmsg = pcall(function()
                    DataStore1:SetAsync(playerUserId2, Data1)
                end)
                if success1 then
                    print("Data saved after kill")
                else
                    print("Error saving data on kill")
                    warn(errmsg)
                end
            end
        end)

    local playerUserId = "Player_".. player.UserId

    --Load Juicy Data
    local Data1
    local Data2
    local success, errormessage = pcall(function()
        Data1 = DataStore1:GetAsync(playerUserId)
        Data2 = DataStore2:GetAsync(playerUserId)
    end)

    if success then 
        overallkills.Value = Data1
        overalldeaths.Value = Data2
        ATK.Value = Data1
        ATD.Value = Data2
    end

end)

game.Players.PlayerRemoving:Connect(function(player)
    local playerUserId = "Player_"..player.UserId
    local Kills = player.leaderstats.Kills
    local Deaths = player.leaderstats.Deaths
    local overallkills = player.leaderstats.Kills.overallkills
    local overalldeaths = player.leaderstats.Deaths.overalldeaths
    local Data1 = overallkills.Value
    local Data2 = overalldeaths.Value

    local success, errormsg = pcall(function()
        DataStore1:SetAsync(playerUserId, Data1)
        DataStore2:SetAsync(playerUserId, Data2)
    end)

    if success then
        print("Data saved")
    else
        print("Error saving data")
        warn(errormsg)
    end
end)

0
It does not print on line 42 after the first death. BossVsLegend -4 — 2y
0
try using defining the character with player.Character or using the characteradded event bdam3000 125 — 2y

Answer this question