Different strategy for achievement system?

So my script is not working. I want to know what I should do differently, and why it isn’t working.
My local script:

local player = game.Players.LocalPlayer

script.Parent.Progress.Text = player.Achievements["Cocoa Collecting 1"].Value.." / 20"

while wait(1) do
	if player.Achievements["Cocoa Collecting 1"].Value >= script.Parent.Max.Value then
		script.Parent.Progress.Text = "Achievement Completed!"
		
	elseif player.Achievements["Cocoa Collecting 1"].Value < script.Parent.Max.Value then
		script.Parent.Progress.Text = player.Achievements["Cocoa Collecting 1"].Value.." / 20"
	else
		warn("Something happened")
	end
end

My script:

local player = script.Parent.Parent.Parent.Parent.Parent.Parent
if player ~= nil then
	if player.Achievements["Cocoa Collecting 1"].Value >= script.Parent.Max.Value then
		script.Parent.Completed.Value = true
		
		player.leaderstats.Crystals.Value = player.leaderstats.Crystals.Value + 10
		local playerMoney = game.ServerStorage.PlayerMoney:FindFirstChild(player.Name)
		if playerMoney ~= nil then
			playerMoney.Value = playerMoney.Value + 1000
		end
	end
end

What is wrong? Should I use events? Thank you for helping out.

edit: What is wrong is:

  • script (not local) doesn’t seem to be working
  • nothing comes in the output when I added a print function to see if it worked

Wait… I think my script just won’t work since the player variable is not working. So, how do you get the player?

Can we get some more context please? What errors are you getting? Which script? Where are these scripts in the explorer?

My errors are:
Won’t give me any stats
and here is a pic

instead of using a While Loop, use a .Changed function

What errors are you getting in the output?

Does that script actually work? You should have LocalScript’s running on the client (so inside startergui or starterplayercripts) and Scripts running on the server (workspace, ServerScriptService)

There are no errors it just doesn’t work

Can you describe what you want the code to do?

I want the code to give me some stats (cash, crystals) when I finish an achievement.

For a .Changed function, do I do

player.Achievements["Cocoa Collecting 1"].Value.Changed

or should I do

player.Achievements["Cocoa Collecting 1"].Changed

You can also use this:

player.Achievements["Cocoa Collecting 1"]:GetPropertyChangedSignal("Value"):Connect()
2 Likes