Leaderstats not changing

Hey guys,
today i was messing around in studio for fun, and i tried to change my leaderstats with a script, but it didnt change. the script is inside the textlabel:

script.Parent.MouseButton1Click:Connect(function()
	game.Players.LocalPlayer.leaderstats.Cash += 50
end)

it just does nothing. thanks for help!

could you show us the leaderstats script of yours?

It should be:

script.Parent.MouseButton1Click:Connect(function()
	game.Players.LocalPlayer.leaderstats.Cash.Value += 50
end)

You forgot to increment the ‘Value’ property of the ‘Cash’ object.

Do you have a script that sets up leaderstats?

when i added .Value it says attempt to index nil with leaderstats :man_shrugging:

yep. they appear in the leaderboard without any problems. (also checked the names)

1 Like

definetely a problem with the leaderstats script of yours

1 Like

That sounds like you are trying to access the LocalPlayer via a Script instead of a LocalScript.

--scripted by anirudh851
--this script saves and loads player's money

local DataStoreService = game:GetService("DataStoreService")
local PlayerCashDatastore = DataStoreService:GetDataStore("PlayerCashDatastore")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	local cash = Instance.new("IntValue", leaderstats)
	cash.Name = "Cash"
	local data
	local success, errormessage = pcall(function()
		data = PlayerCashDatastore:GetAsync(player.UserId.."-cash")
	end)
	if success then
		cash.Value = data
	else
		print("There was an error getting your data:")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		PlayerCashDatastore:SetAsync(player.UserId.."-cash", player.leaderstats.Cash.Value)
	end)
	if success then
		print("Data succesfully saved")
	else
		print("There was an error saving your data:")
		warn(errormessage)
	end
end)

its pretty long as it saves and loads the data from data stores too

im pretty sure u can access localplayer with a script. i have done it countless times before, but still ill change it to a localscript and reply back.

1 Like

hmm the leaderstats script is good i guess its in the gui script make sure the script in the gui is a LocalScript

That is not possible, since the server does not have a player that is local to the server. I think that this used to work in Roblox Studio back in the day, but it will not work in a server.

you can never access local player in a regular script aka serverscript aka script

solved. changed to local and its solved now. i feel like am dumb. thanks for ur time guys.