So I have a skip stage system in my game. I have a GUI. Inside that GUI, I have a local script.
There is a limits variable which checks how many skip stages do you have left. Default is 3. So whenever a player uses a skip stage, the game checks if he has at least 1 skip stage left and if he does, the game reduces the limits variable by 1. So each time the player uses a skip stage, the limits variable will be reduced.
So the problem was that when a player leaves the game, the limits variable resets to 3. Which I don't want. I want the limits variable to stay at the amount at which it was before the player left the game. So I tried using data stores to see if it will work. I am not sure if it will work or not.
But the output says that you cannot access data stores from client i.e. local scripts.
So I thought of using remote events to do the trick. I know how to use remote events but not with data stores. I am a beginner/intermediate in scripting so please help.
Here is what I tried:
My Local Script inside the Skip Stage GUI:
local dataStoreService = game:GetService("DataStoreService") -- Calling DataStoreService local limitsData = dataStoreService:GetDataStore("NameThisWhateverYouWant") -- Making a new datastore called NameThisWhateverYouWant local player = game.Players.LocalPlayer local leaderstats = player:WaitForChild("leaderstats"):FindFirstChild("Checkpoint") local hasLimits = true local Limits = 3 -- This Limits is the amount of skip stages a player has. It resets to 3 whenever they join the game. Which I don't want. local maxLevels = game.Workspace.MaxStages.Value -- This is the total amount of stages in the game. game.Players.PlayerAdded:Connect(function() -- When a player is added Limits = limitsData:GetAsync("AnyKeyYouWant") -- Getting the data that is saved. print("Limits left:", Limits) -- Printing the data that is saved! end) script.Parent.MouseButton1Click:connect(function() if hasLimits == true then if Limits ~= 0 then if leaderstats.Value < maxLevels then Limits = Limits - 1 leaderstats.Value = leaderstats.Value + 1 if player ~= nil then if player.Character ~= nil then if player.Character:FindFirstChild("Humanoid").Health > 0 then player.Character.HumanoidRootPart.CFrame = game.Workspace.Checkpoints:FindFirstChild(leaderstats.Value).CFrame + Vector3.new(0,3,0) end end end end else if player ~= nil then if player.Character ~= nil then if player.Character:FindFirstChild("Humanoid").Health > 0 then player.Character.HumanoidRootPart.CFrame = game.Workspace.Checkpoints:FindFirstChild(leaderstats.Value).CFrame + Vector3.new(0,3,0) end end end end end end) game.Players.PlayerRemoving:Connect(function() -- When a player exits the game print("Saving number of limits:", Limits) -- Printing the limits that are going to be saved limitsData:SetAsync("AnyKeyYouWant", Limits) -- Saving the limits to a key. The key is used to retrieve data when called. end)
P.S: Do you think I am implementing the data stores correctly?
And how do I make the above code with remote events so that I can use data stores via local scripts?
Thanks
To help prevent exploits, you can set it up like this:
Server:
Client:
There is a tutorial for how to use data stores here: https://developer.roblox.com/en-us/articles/Saving-Player-Data