Issue with DataStore

Hello! I seem to have a problem with my DataStore. It was working fine until I added the “WalkSpeed” save. Here is the script… The output also says that either Humanoid or Character isn’t part of player/character and using WaitForChild() and FindFirstChild() didn’t work either.

local ServerStorage = game:GetService("ServerStorage")
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Points = Instance.new("IntValue")
	Points.Name = "Points"
	Points.Parent = leaderstats
	
	local Wins = Instance.new("IntValue")
	Wins.Name = "Wins"
	Wins.Parent = leaderstats
	
	local Activations = Instance.new("IntValue")
	Activations.Name = "Activations"
	Activations.Parent = leaderstats
	
	local Deaths = Instance.new("IntValue")
	Deaths.Name = "Deaths"
	Deaths.Parent = leaderstats
	
	local Upgrades = Instance.new("Folder")
	Upgrades.Name = "Upgrades"
	Upgrades.Parent = player
	
	local SpeedLevel = Instance.new("IntValue")
	SpeedLevel.Name = "SpeedLevel"
	SpeedLevel.Parent = Upgrades

	local JumpLevel = Instance.new("IntValue")
	JumpLevel.Name = "JumpLevel"
	JumpLevel.Parent = Upgrades

	-->> Data: Begining
	
	local playerUserId = "Player_"..player.UserId
	
	-->>Data: Load Data
	
	local data
	local success, errormessage	 = pcall(function()
		data = myDataStore:GetAsync(playerUserId)
	end)
	
	if success then
		if data then
			Points.Value = data.Points
			Wins.Value = data.Wins
			Activations.Value = data.Activations
			Deaths.Value = data.Deaths
			SpeedLevel.Value = data.SpeedLevel
			JumpLevel.Value = data.JumpLevel
			player.Character.Humanoid.WalkSpeed = data.WalkSpeed
		end
	end
	
end)

-->> Data: Saving Stats

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player_"..player.UserId

	local data = {
		Points = player.leaderstats.Points.Value;
		Wins = player.leaderstats.Wins.Value;
		Activations = player.leaderstats.Activations.Value;
		Deaths = player.leaderstats.Deaths.Value;
		SpeedLevel = player.Upgrades.SpeedLevel.Value;
		JumpLevel = player.Upgrades.JumpLevel.Value;
		WalkSpeed = player.Character.Humanoid.WalkSpeed;
	}
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(playerUserId, data)
	end)
		
	if success then
		print("Data succefully saved!")
	else
		print("Data saving error")
		warn(errormessage)
	end
	
end)

That may be due because the character hasn’t been loaded yet and/or the appearance wasn’t loaded yet.

Don’t do that, here’s what you should do, since the character most likely hasn’t loaded.

...

local character = player.Character or player.CharacterAdded:Wait()
character:WaitForChild("Humanoid").WalkSpeed = data.WalkSpeed

He can use the CharacterAppearanceLoaded event in the following way:

player.CharacterAppearanceLoaded:Wait() -- this basically waits until the event is fired meaning it will yield the script until the appearance of the character is loaded.
player.Character.Humanoid.WalkSpeed = data.WalkSpeed

So would puting this in the data loading would work? I added character as first line of the PlayerAdded function.

if character then
       character:WaitForChild("Humanoid").WalkSpeed = data.WalkSpeed
end

No, you can just do the same thing I did, basically how it works is that it checks if the players character exists, if it doesn’t then it waits for the CharacterAdded event to fire. TL;DR, there’s no if statements needed, since it already takes care of waiting for the player’s character.

So how would I add this in my script?

You put it at the part where it says:

player.Character.Humanoid.WalkSpeed = data.Walkspeed

(Without the “…”)

But isn’t that what I had at first?

No, like this:


local ServerStorage = game:GetService("ServerStorage")
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Points = Instance.new("IntValue")
	Points.Name = "Points"
	Points.Parent = leaderstats
	
	local Wins = Instance.new("IntValue")
	Wins.Name = "Wins"
	Wins.Parent = leaderstats
	
	local Activations = Instance.new("IntValue")
	Activations.Name = "Activations"
	Activations.Parent = leaderstats
	
	local Deaths = Instance.new("IntValue")
	Deaths.Name = "Deaths"
	Deaths.Parent = leaderstats
	
	local Upgrades = Instance.new("Folder")
	Upgrades.Name = "Upgrades"
	Upgrades.Parent = player
	
	local SpeedLevel = Instance.new("IntValue")
	SpeedLevel.Name = "SpeedLevel"
	SpeedLevel.Parent = Upgrades

	local JumpLevel = Instance.new("IntValue")
	JumpLevel.Name = "JumpLevel"
	JumpLevel.Parent = Upgrades

	-->> Data: Begining
	
	local playerUserId = "Player_"..player.UserId
	
	-->>Data: Load Data
	
	local data
	local success, errormessage	 = pcall(function()
		data = myDataStore:GetAsync(playerUserId)
	end)
	
	if success then
		if data then
			Points.Value = data.Points
			Wins.Value = data.Wins
			Activations.Value = data.Activations
			Deaths.Value = data.Deaths
			SpeedLevel.Value = data.SpeedLevel
			JumpLevel.Value = data.JumpLevel
			local character = player.Character or player.CharacterAdded:Wait()
            character:WaitForChild("Humanoid").WalkSpeed = data.WalkSpeed
		end
	end
	
end)

-->> Data: Saving Stats

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player_"..player.UserId

	local data = {
		Points = player.leaderstats.Points.Value;
		Wins = player.leaderstats.Wins.Value;
		Activations = player.leaderstats.Activations.Value;
		Deaths = player.leaderstats.Deaths.Value;
		SpeedLevel = player.Upgrades.SpeedLevel.Value;
		JumpLevel = player.Upgrades.JumpLevel.Value;
		WalkSpeed = player.Character.Humanoid.WalkSpeed;
	}
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(playerUserId, data)
	end)
		
	if success then
		print("Data succefully saved!")
	else
		print("Data saving error")
		warn(errormessage)
	end
	
end)

add player.CharacterAppearanceLoaded:Wait() before applying the WalkSpeed.

That’ll wait until the character appearance has loaded and then will resume executing the script.

This doesn’t fix the issue with data not saving at all… So i can’t verify that this works.

Well, are you going into the server view in studio?
And do you have Studio Access to API Services enabled in your game settings?

Yes, it’s on. But what will going into Server Side change?

I don’t have an image readily available, but there’s a button when you go into play solo that says (something like) client, if you click it you can go to the server view.

1 Like

I know, but how will that help?

1 Like

This didn’t seem to work… 30 chars

1 Like

You change the values from the server, you’re probably changing it from the client. With FilteringEnabled, changes from the client don’t replicate.

1 Like

Ohhhh, I see what you mean. I tried changing the Values from the Server side but that did NOT save them sadly…

That’s super bizarre, I have no idea what could be happening.

1 Like