Hi why datastore script getting error

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    i want to fix my script
  2. What is the issue? Include screenshots / videos if possible!
    my CurrentXP value and my MaxXp value keep setting to 0

heres my script

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		wait(.1)
	local Settings = Instance.new("Folder")
	Settings.Name  = "Settings"
	Settings.Parent = plr
	local Lv = Instance.new("IntValue")
	Lv.Name  = "Level"
	Lv.Parent = Settings
	local Stamina = Instance.new("IntValue")
	Stamina.Name = "Stamina"
		Stamina.Parent = Settings
		local MaxStamina = Instance.new("IntValue")
		MaxStamina.Name ="MaxStamina"
		MaxStamina.Parent = Settings
		local Beli 	= Instance.new("IntValue")
		Beli.Name = "Beli"
		Beli.Parent  = Settings
		local CurrentXP = Instance.new("IntValue")
		CurrentXP.Name = "CurrentXP"
		CurrentXP.Value = 0
		CurrentXP.Parent = Settings
		local MaxXP = Instance.new("IntValue")
		MaxXP.Name = "MaxXP"
		MaxXP.Parent = Settings
		MaxStamina.Value= 100
		MaxXP.Value = 100
		Stamina.Value = MaxStamina.Value
		Beli.Value = 1000
		Lv.Value = 1
	local Data = nil
	local succes , errr = pcall(function()   
		Data = MyDataStore:GetAsync(plr.UserId)
	end)	
	
	if succes then
			Lv.Value =  Data.Level 
			MaxXP.Value = Data.MaxXP
			CurrentXP.Value = Data.CurrentXP 
			Beli.Value = Data.Beli
			if MaxStamina ~= 0 then
				MaxStamina.Value = Data.MaxStamina     
			else
				plr:Kick("Anti Cheat")
			end
			if Stamina.Value < MaxStamina.Value then
				plr:Kick("Stamina error report to admin !")
				
			end
		-- Data here
		
		
	else
		warn(errr)
	end
	if Lv.Value > 450  then
		plr:Kick("Hacking ")
	else
		warn("Accepted")
end
		
2 Likes

its no printing error at the roblox studio output

If you test it on studio. Then did not have error, please test on roblox

ok, sure thx for your help XXD

There’s a whole lot going on here. For one, the data reading should not be when the character is added. It should be taken outside of that function, and moved to only the PlayerAdded event function. Secondly, your script fails to check if a player does not have data. As far as I know, the data could be set to nil and still return successful. Lastly, I see no data saving. I see where you attempt to load the data, however it is cached interestingly and saved nowhere in the script.

so do u have any solustion to fix ?

I thought it was pretty self-explanatory.

  • Check and set default values if the player has no previous saves.
  • Save their data when they leave, or periodically if you wish.

Here are some helpful resources for you:
Saving Player Data
Data Stores
Data Store Update Async


Keep in mind the DevForum is not a place where people should write scripts for you. You need to tinker around with it and try to figure things out for yourself. My job is to point you in the right direction, which I have done. Best of luck to you.

You should not be loading data when the character is added …

As Galactiq said we can not write the scripts for you.

However if you don’t understand what to do may i suggest removing the character added event and it should function correctly :wink:

local DataStoreService = game:GetService("DataStoreService")

local MyDataStore = DataStoreService:GetDataStore("MyDataStore")

You can write scripts for her, it’s just not the intentions of the DevForum. You are in no way obligated to write someone a script.

Oh thanks for letting me know i will bear that in mind :slight_smile:

1 Like

Your script have many error:

  1. Remove the character added function.

  2. Add the DataStoreService value

Here is a fix that should work i haven’t got access to studio rn so i cant test but here you go:

DataStoreService=game:service(‘DataStoreService’)
MyDataStore=DataStoreService:GetDataStore(‘PlayerData’)
game.Players.PlayerAdded:Connect(function(plr)
local Settings = Instance.new(“Folder”)
Settings.Name = “Settings”
Settings.Parent = plr
local Lv = Instance.new(“IntValue”)
Lv.Name = “Level”
Lv.Parent = Settings
local Stamina = Instance.new(“IntValue”)
Stamina.Name = “Stamina”
Stamina.Parent = Settings
local MaxStamina = Instance.new(“IntValue”)
MaxStamina.Name =“MaxStamina”
MaxStamina.Parent = Settings
local Beli = Instance.new(“IntValue”)
Beli.Name = “Beli”
Beli.Parent = Settings
local CurrentXP = Instance.new(“IntValue”)
CurrentXP.Name = “CurrentXP”
CurrentXP.Value = 0
CurrentXP.Parent = Settings
local MaxXP = Instance.new(“IntValue”)
MaxXP.Name = “MaxXP”
MaxXP.Parent = Settings
MaxStamina.Value= 100
MaxXP.Value = 100
Stamina.Value = MaxStamina.Value
Beli.Value = 1000
Lv.Value = 1
local Data = nil
local succes , errr = pcall(function()
Data = MyDataStore:GetAsync(plr.UserId)
end)
if succes then
Lv.Value = Data.Level
MaxXP.Value = Data.MaxXP
CurrentXP.Value = Data.CurrentXP
Beli.Value = Data.Beli
if MaxStamina ~= 0 then
MaxStamina.Value = Data.MaxStamina
else
plr:Kick(“Anti Cheat”)
end
if Stamina.Value < MaxStamina.Value then
plr:Kick(“Stamina error report to admin !”)

		end
else
	warn(errr)
end
if Lv.Value > 450  then
	plr:Kick("Hacking ")
else
	warn("Accepted")

end
end)

Edit: Sorry for bad formatting …

sorry for not posting full script i aready did it

1 Like

thx you guys! i just fixed it by myself thx for telling my script problems :blush:

1 Like

thx! i will give you a green mark UwU