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

How do I save these leaderstats?

Asked by 1 year ago

Hello! I'm currently making a game that requires leaderstats, (Trolls, Gems, Rebirths) and I am attempting to find a way to save these. I have tried watching some tutorials/youtube videos, yet none of them work.

Here's my leaderstats script:

game.Players.PlayerAdded:Connect(function(Player)

    if not Player:FindFirstChild("leaderstats") then
        local Folder = Instance.new("Folder" ,Player)
        Folder.Name = "leaderstats"
    end

    local Folder = Player:WaitForChild("leaderstats" ,5)

    local Value1 = Instance.new("IntValue",Folder)
    local Value2 = Instance.new("IntValue",Folder)
    local Value3 = Instance.new("IntValue",Folder)


    Value3.Name = "Trolls"
    Value1.Name = "Gems"
    Value2.Name = "Rebirths"

end)

Any help or advice would be heavily appreciated!

Thanks

0
I updated my answer it should work now theking66hayday 841 — 1y
0
turn on API services in game setting under security theking66hayday 841 — 1y

2 answers

Log in to vote
0
Answered by 1 year ago
Edited 1 year ago

Here is a simply datastore system I did the first 2 for you. For practise try doing the 3rd.

Old Answer Sorry I made some mistypes this should work now I tested it btw Script:

local DSS = game:GetService("DataStoreService")

local Troll = DSS:GetDataStore("TrollData")

local Gems= DSS:GetDataStore("GemsData")--Duplicate this and change name to rebirths



game.Players.PlayerAdded:Connect(function(player)

    local leaderstats = Instance.new("Folder", player)

    leaderstats.Name = "leaderstats"



    local Troll = Instance.new("IntValue", leaderstats)

    Troll.Name = "Troll"



    local gems = Instance.new("IntValue", leaderstats)--Duplicate this and change name to rebirths

    gems.Name = "Gems"--Duplicate this and change name to rebirths



    local playerId = "Player_"..player.UserId



    local Trolldata

    local Gemsdata--Duplicate this and change name to rebirths

    local success, errormessage = pcall(function()

        Trolldata = Troll:GetAsync(playerId)

        Gemsdata = Gems:GetAsync(playerId)--Duplicate this and change name to rebirths

    end)



    if success then

        Troll.Value = Trolldata

        Gems.Value = Gemsdata--Duplicate this and change name to rebirths

    end

end)



game.Players.PlayerRemoving:Connect(function(player)

    local playerId = "Player_"..player.UserId



    local Trolldata = player.leaderstats.Troll.Value

    local Gemsdata = player.leaderstats.Gems.Value--Duplicate this and change name to rebirths



    local success, errormessage = pcall(function()

        Troll:SetAsync(playerId, Trolldata)

        Gems:SetAsync(playerId, Gemsdata)--Duplicate this and change name to rebirths

    end)



    if success then

        print("Saved")

    else

        print("There was an error")

        warn(errormessage)

    end

end)

Hope this helps!!

Updated Script

local DSS = game:GetService("DataStoreService")

local Troll = DSS:GetDataStore("TrollData")

local Gems= DSS:GetDataStore("GemsData")--Duplicate this and change name to rebirths

local Rebirth = DSS:GetDataStore("RebirthData")


game.Players.PlayerAdded:Connect(function(player)

    local leaderstats = Instance.new("Folder", player)

    leaderstats.Name = "leaderstats"



    local Troll = Instance.new("IntValue", leaderstats)

    Troll.Name = "Troll"



    local gems = Instance.new("IntValue", leaderstats)--Duplicate this and change name to rebirths

    gems.Name = "Gems"--Duplicate this and change name to rebirths

local Rebirth = Instance.new("IntValue", leaderstats)

    Rebirth.Name = "Rebirth"

    local playerId = "Player_"..player.UserId



    local Trolldata

    local Gemsdata--Duplicate this and change name to rebirths
local Rebirthdata

    local success, errormessage = pcall(function()

        Trolldata = Troll:GetAsync(playerId)

        Gemsdata = Gems:GetAsync(playerId)--Duplicate this and change name to rebirths
Rebirthdata = Rebirth:GetAsync(playerId)
    end)



    if success then

        Troll.Value = Trolldata

        Gems.Value = Gemsdata--Duplicate this and change name to rebirths
        Rebirth.Value = Rebirthdata
    end

end)



game.Players.PlayerRemoving:Connect(function(player)

    local playerId = "Player_"..player.UserId



    local Trolldata = player.leaderstats.Troll.Value

    local Gemsdata = player.leaderstats.Gems.Value--Duplicate this and change name to rebirths
    local Rebirthdata = player.leaderstats.Rebirth.Value


    local success, errormessage = pcall(function()

        Troll:SetAsync(playerId, Trolldata)

        Gems:SetAsync(playerId, Gemsdata)--Duplicate this and change name to rebirths
Rebirth:SetAsync(playerId, Rebirthdata)
    end)



    if success then

        print("Saved")

    else

        print("There was an error")

        warn(errormessage)

    end

end)

0
Also I just notice Troll in my script isn't plural hope that is ok theking66hayday 841 — 1y
0
Still doesnt work after using the updated script. It says in output 'Saved' when I leave, but it doesn't load it back in once I join back xbantixx 10 — 1y
0
turn on API services in game setting under security theking66hayday 841 — 1y
0
I have xbantixx 10 — 1y
View all comments (11 more)
0
they on?? Google it if you need help lots of tutorials on how to turn them on theking66hayday 841 — 1y
0
Also try testing the script before you add 3rd leaderstat maybe u added it wrong theking66hayday 841 — 1y
0
i added it all correct i did what it told me to do xbantixx 10 — 1y
0
weird it works for me though theking66hayday 841 — 1y
0
maybe try send me the script with rebirths already because I might be making a mistake and I dont realise xbantixx 10 — 1y
0
OK theking66hayday 841 — 1y
0
I updated my answer copy the Updated script theking66hayday 841 — 1y
0
Before GetAsync, Troll is defined as an Instance, not a DataStore. 9mze 193 — 1y
0
so how would i fix that? xbantixx 10 — 1y
0
Opps line 46 where it says Rebirthdata = Troll change Troll to Rebirth theking66hayday 841 — 1y
0
That should fix it I will update my script theking66hayday 841 — 1y
Ad
Log in to vote
0
Answered by 1 year ago

Doesn't seem to work. It has red lines under every time it says TrollData, GemsData and RebirthsData (the new one I made for rebirths). I don't know why. An answer would be appreciated

0
Show us the error output xXLegendGamerz16Xx 231 — 1y
0
There was no error in the output, it just didn't work xbantixx 10 — 1y
0
I updated my answer try it theking66hayday 841 — 1y

Answer this question