#Scripting Stage in roblox studio

1 messages · Page 1 of 1 (latest)

strange canyon
#

Basically im trying to* code this system* where you touch a stage it adds to your stats (i already done coding that) what i actually need help with is the **DataStoreService ** code. When the player leaves their stats do not save. That is all i need help from

#

local DataStoreService = game:GetService("DataStoreService")
local PlayerStage = DataStoreService:GetDataStore("PlayerStage")
local Stage1 = game.Workspace.Stage:WaitForChild("1")
local DEFAULT_STAGE = 1

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

local Stage = Instance.new("IntValue", leaderstats)
Stage.Name = "Stage"




Stage1.Touched:Connect(function()
    Stage.Value = 1
    local success, error = pcall(function()
        return PlayerStage:GetAsync(player.UserId)
        
    end)
    
    if success then
        Stage.Value = 1
        print("Stage is working.")
    else
        print("Failed to load Default stage.")
    end
    
end)

game.Players.PlayerRemoving:Connect(function(player)
    local success, error = pcall(function()
        return PlayerStage:SetAsync(player.UserId, player.leaderstats.Stage.Value)
         
    end)
    if success then
        print("Stage is saved.")
        Stage.Value = DEFAULT_STAGE
    else
        print("Failed to save stage.")
        DEFAULT_STAGE = false
        Stage.Value = 0
    end
end)

end)

#

Heres the code for it.

#

Altho i do not know how to write code styles.

severe thorn
#

if you are giving stages from the client it will always save 0

#

PlayerRemoving is inside of playeradded

#

DEFAULT_STAGE was supposed to be a number but in line 45 u set it to false

#

and you are using Touched on player added too

#

and you are only loading the data when the player touches the stage

#

because it is on a touched event

#

and even if it saves correctly it will only show "1"

#

because for some reason you set it to 1 if it was sucessful

#

and there is alot of mistakes there

crystal thicket
crystal thicket
crystal thicket