#Problem with saving value in module script

16 messages · Page 1 of 1 (latest)

dusk siren
#

So im new to module scripts and i made a script that works and it has values but i want to save the values and idk how here is the script i made

Saving Script:

local DataStoreService = game:GetService("DataStoreService") --Gets the DataStoreService
local Rs = game:GetService("ReplicatedStorage")
local dataStore = DataStoreService:GetDataStore("Test1") --Gives the Datastore a name

local UpgradesModule = require(Rs:WaitForChild("Modules"):WaitForChild("ModuleScript"))

--//Function
local function saveData(player) 
    local tableToSave = {
        UpgradesModule.Variables.Speed;
    }

    local success, errorMessage = pcall(dataStore.SetAsync, dataStore, player.UserId, tableToSave) 

    if success then 
        print("Data2 has been saved!") 
    else 
        print("Data2 has not been saved!") 
    end
end

game.Players.PlayerAdded:Connect(function(player)
    local data = nil 

    local success, errorMessage = pcall(function() 
        data = dataStore:GetAsync(player.UserId)
    end)

    if success and data then 
        UpgradesModule.Variables.Speed = data[1]
    else
        print("The Player has no Data!") 
        warn(errorMessage)
    end
end)

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

game:BindToClose(function() 
    for _, player in ipairs(game.Players:GetPlayers()) do 
        task.spawn(saveData, player) 
    end
end)
#

and heres the module script:

local plr = game.Players.LocalPlayer
local RS = game:GetService("ReplicatedStorage")

local Remotes = RS:WaitForChild("Remotes")

local module = {
    
    Variables = {
        --Speed
        Speed = 0;
        MaxSpeed = 10;
        --Multiplier
        Multiplier = 1;
        MaxMultiplier = 10;
        --Cost
        Cost = 0;
    }
}

function module.Speed()
    if module.Variables.Speed < module.Variables.MaxSpeed then
        if plr.leaderstats.Coins.Value >= module.Variables.Cost then
            plr.leaderstats.Coins.Value -= module.Variables.Cost
            module.Variables.Speed += 1
            plr.Character.Humanoid.WalkSpeed += 10
        end
    end
end

return module
glossy dust
dusk siren
#

My goal was to make like a character upgrade

#

Like I can buy more speed for my character

#

So like i buy it I get + 10 speed then it says 1/10

#

But when I leave and rejoin it’s 0 so I’m trying to save the speed value

#

Speed value is the 1/10 2/10 etc

glossy dust
#

why not add prints to the system

dusk siren
#

Wym

#

I’m trying to make it save my data and it prints saying it saved my data but it doesn’t

glossy dust
#

so why not create a IntValue and make the IntValue = the Module.Value then Module.Value = IntValue.Value

#

like lua local SpeedUp = Instance.new('IntValue',FolderName) SpeedUp.Name = 'Speed Upgrade' SpeedUp.Value = 0 SpeedUp.Value = Module.SpeedSet Module.SpeedSet = SpeedUp.Value

dusk siren
#

Ohhh