Script doesn't change the leaderstats

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!
    What I want to do is, when a part named “MoneyPart” touches a “FunctionPart”, the script inside it should add the value of an int value which is inside the “MoneyPart” and player’s “Money” value.

  2. What is the issue? Include screenshots / videos if possible!
    The issue is that function finds player, finds the value of the “MoneyPart”, finds the “Money” leaderstat but it doesn’t add the values together

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried to find but I couldn’t find anything

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local Players = game.Players
local FunctionPart = script.Parent

local function onTouch(hit)
	if hit.Name == "MoneyPart" then
		local owner = FunctionPart.Parent.Parent.Person.Value --This is a value that specifies the tycoon owner
		local player = Players:FindFirstChild(owner)
		
		if player then
			local moneyPartValue = hit.Worth.Value--This is the value of a money part

			local moneyStat = player.leaderstats.Money.Value

			print(moneyStat)--To check if script gets the value
			print(moneyPartValue)--To check if script gets the value
			moneyStat = moneyStat + moneyPartValue

			hit:Destroy()
		else
			hit:Destroy()
		end
	end
end

FunctionPart.Touched:Connect(onTouch)

It prints the values correctly and gives no errors

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

You’re storing the value inside of moneyStat. You have to do

local moneyStat = player.leaderstats.Money
moneyStat.Value = moneyStat.Value + moneyPartValue
1 Like

They both change. If a player leaves tycoon owner value becomes empty and stays empty until a player joins a game. Money part value depends on dropper and upgrade things

1 Like

Thank you so much. I learned a new thing today!