Hello Developers!
I am attempting to save the preferences of player settings but have no idea how to even start.
How could i save player data/toggled or not so for example: if a player does not want shadows they only have to toggle it once and dont have to turn it off every time they join the game.
Here is the example file:
TopBarExample.rbxm (66.3 KB)
Here is the Code:
Summary
local container = script.Parent
local Icon = require(container.Icon)
–Services
local LightingService = game:GetService(“Lighting”)
local DataStoreService = game:GetService(“DataStoreService”)
–Variables
local Shadows = true
local AutoRun = true
local AutoUse = true
–Settings
Icon.new()
:setLabel(“”)
:setImage(14134158045, “Deselected”)
:setImage(14134158045, “Selected”)
:bindToggleKey(Enum.KeyCode.K)
:setCaption(“Settings”)
:setDropdown({
Icon.new()
:autoDeselect(false)
:setLabel(“AutoRun”)
:setImage(14930908086, “Deselected”)
:setImage(10175169779, “Selected”),
Icon.new()
:autoDeselect(false)
:setLabel(“AutoUse”)
:setImage(10175169779, “Deselected”)
:setImage(14930908086, “Selected”),
Icon.new()
:autoDeselect(false)
:setLabel(“Shadows”)
:setImage(10175169779, “Deselected”)
:setImage(14930908086, “Selected”)
:bindEvent(“selected”,function()
LightingService.GlobalShadows = false
end)
:bindEvent(“deselected”,function()
LightingService.GlobalShadows = true
end)
})
- First, you need to create a DataStore. You can name it whatever you want, like “Settings”.
- Then, you need to get the settings. For the GetAsync, I would recommend to follow this format :
local Data
local suc, err = pcall(function()
Data = MyDataStore:GetAsync(key)
end)
That way, you can catch every errors (suc
represents if the function had any errors during the execution → true
= no error; false
= error(s). err
is the response, the error will be saved here if there is one.)
Notes : Data
is the data that you saved. The key
should be different for each player, I would recommend setting the UserId as the key.
- Then, you would set the in game settings according to the data
- Lastly, when the user changes his setting, you need to save it with SetAsync or UpdateAsync You need to modify these functions :
Feel free to ask questions.
i dont know how to use datastore at all like not even a bit
also, it throws a error since the script is on the client,
datastore cannot be accessed from client
this is what i did
Summary
local Players = game:GetService(“Players”)
local player = Players:GetPlayerByUserId(1)
local MyDataStore = DataStoreService:GetGlobalDataStore()
local Data
local suc, err = pcall(function()
Data = MyDataStore:GetAsync(player)
end)
and for the functions
Summary
:bindEvent("selected",function()
LightingService.GlobalShadows = false
MyDataStore:SetAsync()
end)
:bindEvent("deselected",function()
LightingService.GlobalShadows = true
MyDataStore:SetAsync()
end)
})