How to use data stores via local script through remote events/functions?
Asked by
4 years ago Edited 4 years ago
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:
01 | local dataStoreService = game:GetService( "DataStoreService" ) |
02 | local limitsData = dataStoreService:GetDataStore( "NameThisWhateverYouWant" ) |
04 | local player = game.Players.LocalPlayer |
05 | local leaderstats = player:WaitForChild( "leaderstats" ):FindFirstChild( "Checkpoint" ) |
08 | local maxLevels = game.Workspace.MaxStages.Value |
10 | game.Players.PlayerAdded:Connect( function () |
11 | Limits = limitsData:GetAsync( "AnyKeyYouWant" ) |
12 | print ( "Limits left:" , Limits) |
16 | script.Parent.MouseButton 1 Click:connect( function () |
17 | if hasLimits = = true then |
19 | if leaderstats.Value < maxLevels then |
21 | leaderstats.Value = leaderstats.Value + 1 |
23 | if player.Character ~ = nil then |
24 | if player.Character:FindFirstChild( "Humanoid" ).Health > 0 then |
26 | player.Character.HumanoidRootPart.CFrame = game.Workspace.Checkpoints:FindFirstChild(leaderstats.Value).CFrame + Vector 3. new( 0 , 3 , 0 ) |
34 | if player.Character ~ = nil then |
35 | if player.Character:FindFirstChild( "Humanoid" ).Health > 0 then |
36 | player.Character.HumanoidRootPart.CFrame = game.Workspace.Checkpoints:FindFirstChild(leaderstats.Value).CFrame + Vector 3. new( 0 , 3 , 0 ) |
46 | game.Players.PlayerRemoving:Connect( function () |
48 | print ( "Saving number of limits:" , Limits) |
49 | limitsData:SetAsync( "AnyKeyYouWant" , Limits) |
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