Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

I need help with my datastore, it only works in studio and not in game?

My data store only works in studio and not in the game

local player = game.Players

local DataStore = game:GetService("DataStoreService")
local ds1 = DataStore:GetDataStore('New')

game.Players.PlayerAdded:connect(function(player)
	local ldr = game.Players.LocalPlayer:WaitForChild('ldr')
	local lvl = ldr:WaitForChild('Level')
	local stam = ldr:WaitForChild('StaminaLvl')
	local aura = ldr:WaitForChild('AuraLimitLvl')
	lvl.Value = ds1:GetAsync(player.UserId) or 1
	stam.Value = ds1:GetAsync(player.UserId) or 1
	aura.Value = ds1:GetAsync(player.UserId) or 1



	lvl:GetPropertyChangedSignal('Value'):connect(function()
		ds1:SetAsync(player.UserId,lvl.Value)
		print("save "..lvl.Value)
	end)

	stam:GetPropertyChangedSignal('Value'):connect(function()
		ds1:SetAsync(player.UserId,stam.Value)
		print("save "..stam.Value)
	end)

	aura:GetPropertyChangedSignal('Value'):connect(function()
		ds1:SetAsync(player.UserId,aura.Value)
		print("save "..aura.Value)
	end)	
end)

2 answers

Hey, it seems like you are doing some inefficient things at the same time you forget something important.

After looking at your code, I found a bug...

This is your code that bugs: local player = game.Players You completely forgot to place LocalPlayer, thus erroring it.

Also, you spam GetAsync() a lot, uselessly. Here is a more efficient code and without the bug:

local player = game.Players.LocalPlayer -- You forgot '.LocalPlayer'

local DataStore = game:GetService("DataStoreService")
local ds1 = DataStore:GetDataStore('New')

game.Players.PlayerAdded:connect(function(player)
    local ldr = player:WaitForChild('ldr')
    local lvl = ldr:WaitForChild('Level')
    local stam = ldr:WaitForChild('StaminaLvl')
    local aura = ldr:WaitForChild('AuraLimitLvl')
    local value = ds1:GetAsync(player.UserId) -- Only one instead of thrice per player.
    
    lvl.Value = value or 1
    stam.Value = value or 1
    aura.Value = value or 1

    lvl:GetPropertyChangedSignal('Value'):connect(function()
        ds1:SetAsync(player.UserId,lvl.Value)
        print("save "..lvl.Value)
    end)

    stam:GetPropertyChangedSignal('Value'):connect(function()
        ds1:SetAsync(player.UserId,stam.Value)
        print("save "..stam.Value)
    end)

    aura:GetPropertyChangedSignal('Value'):connect(function()
        ds1:SetAsync(player.UserId,aura.Value)
        print("save "..aura.Value)
    end)    
end)

8 comments

0

But would i just keep it in server script service in a script or move it to a local script?

0

Data stores don't work in localscripts

0

So yes, keep it in ServerScriptService

0

dpark for some reason the everytime one of the value increases the other increases 2

0

well they do if i were to rejoin

0

What is the point of line 1?

0

line 7

0

You defined player on line 6, so no need to write line 1.

I'm hoping this will work.

local DataStore = game:GetService("DataStoreService"):GetDataStore('New') -- Since you're not getting any other datastore, I combined them

game.Players.PlayerRemoving:Connect(function(player) -- This is for saving data when player leaving.
    ds1:SetAsync(player.UserId,player.LeaderStats.Level.Value)
    print("Saved Level")
    ds1:SetAsync(player.UserId,player.LeaderStats.StaminaLevel.Value)
    print("Saved Stamina Level")
    ds1:SetAsync(player.UserId,player.LeaderStats.AuraLimitLvl.Value)
    print("Saved Aura Level")   
end)

game.Players.PlayerAdded:connect(function(player) -- This is for getting data when player joins.
    local ldr = instance.new("IntValue", player)
    ldr.Name = "LeaderStats"
    local lvl = instance.new("IntValue", ldr)
    lvl.Name = "Level"
    local stam = instance.new("IntValue", ldr)
    stam.Name = "StaminaLvl"
    local aura = ldr:WaitForChild('AuraLimitLvl')
    aura.Name = "AuraLimitLvl"
    lvl.Value = ds1:GetAsync(player.UserId) or 1
    stam.Value = ds1:GetAsync(player.UserId) or 1
    aura.Value = ds1:GetAsync(player.UserId) or 1  
end)

2 comments

0

By the way, place this in a Normal Script.

0

And put in in ServerScriptService or Workspace

This discussion is preserved as it was. New answers, comments, and votes are closed.