Still have questions? Join our Discord server and get real time help.
0

# Why is my DataStore2 not saving or changing values?

Edited 9 days ago

I have this field of view slider that changes the cameras field of view, and I am trying to get it to save the field of view sliders position. It was working fine with normal DataStore but doesn't seem to be working with DataStore2.

I followed this AlvinBlox tutorial https://www.youtube.com/watch?time_continue=4&v=hBfMfB0BwGA&feature=emb_logo

This is the data store 2 code:

local SeverScriptService = game:GetService("ServerScriptService")
local DataStore2 = require(SeverScriptService:WaitForChild("DataStore2"))
local DefaultValue = 0.771

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("GuiPositionSave")

local FOVStore = DataStore2("FOVSave", Plr)
--Folder
local SettingsSaveFolder = Instance.new("Folder", Plr)
SettingsSaveFolder.Name = "SettingsData"

--FOV
local FieldOfView = Instance.new("NumberValue", SettingsSaveFolder)
FieldOfView.Name = "FovValue"

local function pointsUpdate(updatedValue)
FieldOfView.Value = FOVStore:Get(updatedValue)

end
pointsUpdate(DefaultValue)

FOVStore:OnUpdate(pointsUpdate)
end)

local function GuiChangerPos(player, GuiPos)
local FOVStore = DataStore2("FovSave", player)
FOVStore:Increment(GuiPos,DefaultValue)

-------------------LINES THAT HAVE BEEN COMMENTED OUT---------------------------------------------
--local FovVal = player:FindFirstChild("SettingsData").FovValue
--FovVal.Value = GuiPos    ---IF I ENABLE THESE LINES THE VALUE CHANGES WHEN I DRAG THE SLIDER
end

local function ResetSliderToDefaultPosition(player, GuiPos)
local FOVStore = DataStore2("FovSave", player)
FOVStore:Increment(GuiPos,DefaultValue)   -

-------------------LINES THAT HAVE BEEN COMMENTED OUT---------------------------------------------
--local FovVal = player:FindFirstChild("SettingsData").FovValue
--FovVal.Value = GuiPos   ---IF I ENABLE THESE LINES THE VALUE CHANGES WHEN I CLICK A BUTTON, WHICH CHANGES THE VALUE BACK THE DEFAULT FOV

end

game.ReplicatedStorage.FOVval.OnServerEvent:Connect(GuiChangerPos)

game.ReplicatedStorage.FOVval2.OnServerEvent:Connect(ResetSliderToDefaultPosition)




this is the local slider script 

local sliderBG = script.Parent

local slider = sliderBG:WaitForChild("Slider")
--local sliderBox = sliderBG:WaitForChild("SliderInput")
local uis = game:GetService("UserInputService")
local Rus = game:GetService("RunService")
local player = game.Players.LocalPlayer
local SettingsStats = player:WaitForChild("SettingsData")
local FovVal = SettingsStats:WaitForChild("FovValue")
local Slider = script.Parent.Slider
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RepRemote = ReplicatedStorage:WaitForChild("FOVval")

--Variables
Slider.Position = UDim2.new(FovVal.Value,0,-4,0)
end

FovVal.Changed:Connect(function() --If value changes
if Slider then
Slider.Position = UDim2.new(FovVal.Value,0,-4,0)
end
end)

local noofstops = 120

local buttonHold = false
local percentage = 0

--actual code
local stops = 1/noofstops
--local stops = 0.01

function dowork(number, value)
if value == 1 then
return number

else
return math.floor(number/value+.5)*value
end

end

uis.InputEnded:Connect(function(key, processed)
if key.UserInputType == Enum.UserInputType.MouseButton1 then
buttonHold = false
end

end)

script.Parent.Slider.TextButton.MouseButton1Down:Connect(function()
buttonHold = true
end)

Rus.RenderStepped:Connect(function(alpha)
if buttonHold then
game.ReplicatedStorage.FOVval:FireServer(script.Parent.Slider.Position.X.Scale) ----EVENT FIRES IF BUTTON HOLD = TRUE. Also sends over the Sliders X position

local FovData = player:WaitForChild("SettingsData")
local FovValue = FovData:WaitForChild("FovValue")
local mouseLocation = uis:GetMouseLocation().X
local buttonPosition = slider.Position
local SliderSize = sliderBG.AbsoluteSize.X
local sliderPosition = sliderBG.AbsolutePosition.X
local position = dowork((mouseLocation-sliderPosition)/SliderSize, stops)
percentage = math.clamp(position,0,1)
slider.Position = UDim2.new(percentage,0,buttonPosition.Y.Scale, buttonPosition.Y.Offset)
end
end)

for _, player in ipairs(game.Players:GetPlayers()) do
end



`

What should Happen here is that that when I drag the slider button it fires a remote event, it should update the the value, but thats just not happening, unless I enable the lines that have been commented out in the data store script. It’s also not saving even though in the output in says it has saved the data. Like when I join the value just resets back to the default FOV

What am I doing wrong? Does DataStore2 not work in studio, even though I have the boolvalue in ServerStorage named “SaveInStudio” set to true. Help would be much appreciated thanks.

0
Just a couple of things to point out: Increment adds the original value to the value you provide, Set will replace the value (it seems like you'll want to use Set instead). The default value should only be used during the original :Get, no need to do it every time you want to add data to the datastore: FOVStore = DataStore2("FOVSave", Plr):Get(DefaultValue) SteelMettle1 304 — 8d
0
Also, you may want to avoid using value instances whenever possible. Instead you can just store the value in a variable, if you need to differentiate between players then use a table like this: numValue[playerName] = 5 just remember to clear out their playerName key when they leave or you'll have a memory leak. SteelMettle1 304 — 8d
0
If I store the value in a variable won't that make it so the value is unable to be changed in a different script? I'm kinda new to lua so not sure how that works, because when I join the game the X.Scale positon of the slider goes to = the value that I saved. ShineBright2775 29 — 8d
0
Thanks for the tips ShineBright2775 29 — 8d
0
now I am getting this error: "ServerScriptService.FOVDataSave:24: attempt to index number with 'Get'" ShineBright2775 29 — 8d
0
It goes away when I take of the :Get(DefaultValue) from "FOVStore = DataStore2("FOVSave", Plr):Get(DefaultValue)" ShineBright2775 29 — 8d
0
Try to change the name of the main key and try again (Caution though, this will "remove" all the saved data in the datastore), you may just have some slight desyncing is all. I have some code that looks exactly like this that works exactly as expected: CurrencyData = DataStore2("Currency", plr):GetTable(defaultCurrencyTable) The default value is a table, hence the :GetTable SteelMettle1 304 — 8d
0
As far as the variable instances, try to keep similar data consolidated into the same script. If you need to access the same data between scripts, then you can use Bindables or ModuleScripts to share the data. Both of these are A LOT better of an option rather than value instances. SteelMettle1 304 — 8d
0
Thanks man seems to be changing values and saving so far. ShineBright2775 29 — 8d
0
No problem bud! I also commented some tips for Datastore2 a while ago in AlvinBlox's video I think. SteelMettle1 304 — 7d