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

Script cannot read leaderstats coins value?

Hey! So I'm trying to make a very basic coin save and load. It is meant to load the player coins when it enters, and save them when it leaves. However it always saves the coins value as 0, and if I try to just read the coins value from a normal script, it reads it as 0 no matter what value is stored. However if I try to read the value from a LocalScript it does read the value.

What is wrong?

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("CoinsSave")

game.Players.PlayerAdded:Connect(function(player)
	local leader = Instance.new("Folder", player)
	leader.Name = "leaderstats"
	local coins = Instance.new("IntValue", leader)
	coins.Name = "Coins"
	local success, coinsData = pcall(
		function()
			coins.Value = ds:GetAsync(player.UserId, player.leaderstats.Coins.Value)		
		end
	)
	if success then
		print ("success")
		else print("Failed")
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	--print("player left")
	local success, err = pcall(
		function()
			--print("func")
			print(player.leaderstats.Coins.Value)
			ds:SetAsync(player.UserId, player.leaderstats.Coins.Value)
		end
	)
	if success then
		print("success")
		else print("Failed")
	end
end)

2 comments

0

You will need RemoteEvent if the client needs to do something the server cannot, such as user input, for the changes to replicate to thes server.

0

Use RemoteEvents.

1 answer

You are changing the value on the client. You will have to use RemoteEvents or RemoteFunctions to change the value on the server so that it replicates.

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