Value not saving when changed!

  1. What do you want to achieve?

A currency trading system.

  1. What is the issue?

The issue is whenever I trade the currency it changes but when i go to collect like a gem it goes back to the original currency!

  1. What solutions have you tried so far?

Finding a solution!

local Player = game.Players.LocalPlayer
local Lightning = game.Lighting
local Blur = Lightning.Blur

script.Parent.MouseButton1Click:Connect(function()
	script.Parent.Parent.Visible = false
	if script.Parent.Parent.COSMETIC.TextLabel.Text == "Yes" then
		script.Parent.Parent.Parent.ButtonN.Visible = false
		game.Players.LocalPlayer.leaderstats.Gems.Value = game.Players.LocalPlayer.leaderstats.Gems.Value + script.Parent.Parent.Parent.Parent.TradingGems.Value
		game.Players.LocalPlayer.leaderstats.Coins.Value = game.Players.LocalPlayer.leaderstats.Coins.Value - script.Parent.Parent.Parent.Parent.TradingCoins.Value
		if script.Parent.Parent.Parent.Parent.Talk.Value < 5 then
			script.Parent.Parent.Parent.Parent.Talk.Value=script.Parent.Parent.Parent.Parent.Talk.Value + 1
		end	
	else
		if script.Parent.Parent.Parent.Parent.Talk.Value < 5 then
			script.Parent.Parent.Parent.Parent.Talk.Value = script.Parent.Parent.Parent.Parent.Talk.Value + 1
		end
	end
end)

Thank you in advance!

it doesnt change since this is a local script.

I changed it to a script and now it is coming up with errors like “attempt to index nil with ‘leaderstats’”

Because game.Players.LocalPlayer doesnt work in server scripts

I changed it to this

local Lightning = game.Lighting
local Blur = Lightning.Blur

script.Parent.MouseButton1Click:Connect(function(Players)
	script.Parent.Parent.Visible = false
	if script.Parent.Parent.COSMETIC.TextLabel.Text == "Yes" then
		script.Parent.Parent.Parent.ButtonN.Visible = false
		Players.leaderstats.Gems.Value = Players.leaderstats.Gems.Value + script.Parent.Parent.Parent.Parent.TradingGems.Value
		Players.leaderstats.Coins.Value = Players.leaderstats.Coins.Value - script.Parent.Parent.Parent.Parent.TradingCoins.Value
		if script.Parent.Parent.Parent.Parent.Talk.Value < 5 then
			script.Parent.Parent.Parent.Parent.Talk.Value = script.Parent.Parent.Parent.Parent.Talk.Value + 1
		end	
	else
		if script.Parent.Parent.Parent.Parent.Talk.Value < 5 then
			script.Parent.Parent.Parent.Parent.Talk.Value = script.Parent.Parent.Parent.Parent.Talk.Value + 1
		end
	end
end)

can you show me how the hierarchy looks like? And where the script is located

image

full hierarchy. not just the script

image

local player = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent
player.leaderstats.Gems.Value = player.leaderstats.Gems.Value + script.Parent.Parent.Parent.Parent.TradingGems.Value
player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - script.Parent.Parent.Parent.Parent.TradingCoins.Value

Error: leaderstats is not a valid member of Players “Players”

local player = script.Parent.Parent.Parent.Parent.Parent.Parent

local Player = script:FindFirstAncestorWhichIsA("Player")
local LS = Player:WaitForChild("leaderstats")
local Gems = LS:WaitForChild("Gems")
local Coins = LS:WaitForChild("Coins")

local Lightning = game:GetService("Lighting")
local Blur = Lightning:WaitForChild("Blur")

local CurrencyGui = script:FindFirstAncestorWhichIsA("ScreenGui")
local Talk = CurrencyGui:WaitForChild("Talk")
local TGems = CurrencyGui:WaitForChild("TradingGems")
local TCoins = CurrencyGui:WaitForChild("TradingCoins")

local Dialogue = CurrencyGui:WaitForChild("Dialogue")
local ButtonN = Dialogue:WaitForChild("ButtonN")
local Button = script.Parent
local ButtonY = Button.Parent
local Cosmetic = ButtonY:WaitForChild("COSMETIC")
local Label = Cosmetic:WaitForChild("TextLabel")

Button.MouseButton1Click:Connect(function()
	ButtonY.Visible = false
	if Label.Text == "Yes" then
		ButtonN.Visible = false
		Gems.Value += TGems.Value
		Coins.Value -= TCoins.Value
		if Talk.Value < 5 then
			Talk.Value += 1
		end	
	else
		if Talk.Value < 5 then
			Talk.Value += 1
		end
	end
end)

Server script, same place it was before.

Its messed up now like it won’t let me go to the next it just keeps giving me the gems