Method updates the wrong value

To initiate OOP isn’t on my strong side so sorry in advance for the folks who are bright in this,
For some ood reason my method updates the wrong value.
Module:

local module = {}
module.__index = module

function module.new(Player: Player)
	local self = setmetatable({}, module)
	
	self.Player = Player
	self.leaderstats = Instance.new("Folder")
	self.leaderstats.Parent = Player
	self.leaderstats.Name = "leaderstats"
	
	return self
end

function module:CreateValue(Name: string, Value: number)
	self.Value = Instance.new("IntValue")
	self.Value.Parent = self.leaderstats
	self.Value.Name = Name or "Currency"
	self.Value.Value = Value or 0
	
	return {
		GetValue = function(_)
			return self.Value.Value
		end,
		SetValue = function(_, Amount: number)
			print(Amount)
			self.Value.Value = Amount or 0
		end,
	}
end

return module

Server:

-- // Variables \\ --
local Services = {
	Players = game:GetService("Players"),
	ReplicatedStorage = game:GetService("ReplicatedStorage")
}

local Modules = {
	PlayerStatsHandler = require(Services.ReplicatedStorage.Modules.leaderstats)
}

-- // Functions \\ --
local function LoadData(Player: Player)
	local leaderstats = Modules.PlayerStatsHandler.new(Player)
	local Time = leaderstats:CreateValue("Time")
	local Coins = leaderstats:CreateValue("Coins")
	
	task.spawn(function()
		while task.wait(1) do
			Time:SetValue(Time:GetValue() + 1)
		end
	end)
end

-- // Connections \\ --
Services.Players.PlayerAdded:Connect(LoadData)

It is supposed to update the Time value but it instead updates the Coins value any fixes to this? I’d be very grateful.

Each time you make a new value, it will overwrite the previous value in the object, because you’re assigning it to the same key.

Basically:

  • Create time value, object now points to Time under Value.
  • Create coins value, object now points to Coins under Value, Time is lost.

You can thank this line for it:

which initially overwrites the value.




To fix this issue, you could store it under unique keys, like this:

function module:CreateValue(Name: string, Value: number)
	local val = Instance.new("IntValue")
	val.Parent = self.leaderstats
	val.Name = Name or "Currency"
	val.Value = Value or 0
    self[Name] = val
	
	return {
		GetValue = function(_)
			return val.Value
		end,
		SetValue = function(_, Amount: number)
			print(Amount)
			val.Value = Amount or 0
		end,
	}
end
1 Like

I am not sure if this helps you but,


local module = {}
module.__index = module

function module.new(Player: Player)
	local self = setmetatable({}, module)

	self.Player = Player
	self.leaderstats = Instance.new("Folder")
	self.leaderstats.Parent = Player
	self.leaderstats.Name = "leaderstats"

	return self
end

function module:CreateValue(Name: string, value : number)
	local Value = Instance.new("IntValue")
	Value.Parent = self.leaderstats
	Value.Name = Name or "Currency"
	Value.Value = value or 0

	return {
		GetValue = function(_)
			return Value.Value
		end,
		SetValue = function(_, Amount: number)
			print(Amount)
			Value.Value = Amount or 0
		end,
	}
end

return module
1 Like

Thank you for the help appreciate !

1 Like

Props to you for going far out to help me really thankful.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.