How can I add a value inside a script?

Hello,
I’m trying to make a script that has a value inside it. Here’s an example

local Name = script.Parent.Value
local True = script.Parent.Parent..Name.Value..Own.Value = true

I know it’s kind of wacky, but how can I do something like that? The objective of this is making a shop. So when the player presses buy I need to change the own value to true. But, the system doesn’t know the name of the exact value because I’m trying to make the shop global and not 1 script per object. I hope it makes sense if not I will try my best to further explain.
Thank you in advance.

Could do with a bit of extra explanation; also on the second line, why are there 2 periods between some values?

I tried adding the “…” because I thought when you add a value it you do this:

local Value = 0
script.Parent.Text == "Hi "..Value

But it ended up not working.
So I have a system when you click the item shop frame which is this:
Screenshot 2021-11-09 082124
It opens another gui which is this:
Screenshot 2021-11-09 082124
Then you press buy. The script for the buy object is this:

local Player = game:GetService("Players").LocalPlayer
local Cash = Player.leaderstats:WaitForChild("Cash")

script.Parent.MouseButton1Down:Connect(function()
	local Name = script.Parent.Parent:WaitForChild("NameV")
	local Price = script.Parent.Parent:WaitForChild("PriceV")
	
	if Price.Value <= Cash.Value then
		Cash.Value -= Price.Value
		print("Bought")
	else
		script.Parent.BackgroundColor3 = Color3.new(1, 0, 0)
		print("Not Enough")
		wait(0.5)
		script.Parent.BackgroundColor3 = Color3.new(0.0392157, 0.0392157, 0.0392157)
	end
end)

But I need to add a own value to see if the player already owns the object.
Also from the object side it changes the values from the buy frame to the object:
Screenshot 2021-11-09 082124

I’d create a folder that we can put BoolValues in, named the items that are owned.

local Folder = <put a folder reference here>

local function isItemOwned(itemName)
    return Folder:FindFirstChild(itemName)
end

local function itemBought(itemName)
    if isItemOwned(itemName) then
        error("Item bought, but is already owned?")
    end

    local ownedValue = Instance.new("BoolValue", Folder)
    ownedValue.Name = itemName
    ownedValue.Value = true
end
1 Like
local Player = game:GetService("Players").LocalPlayer
local Cash = Player.leaderstats:WaitForChild("Cash")
local HasBought = false

script.Parent.MouseButton1Down:Connect(function()
	if HasBought then --if bought already do action and end function early
		--do code
		return
	end
	local Name = script.Parent.Parent:WaitForChild("NameV")
	local Price = script.Parent.Parent:WaitForChild("PriceV")
	if Price.Value <= Cash.Value then
		Cash.Value -= Price.Value
		HasBought = true
		print("Bought")
	else
		script.Parent.BackgroundColor3 = Color3.new(1, 0, 0)
		print("Not Enough")
		wait(0.5)
		script.Parent.BackgroundColor3 = Color3.new(0.0392157, 0.0392157, 0.0392157)
	end
end)

You don’t even need any “BoolValue” instances to achieve this, just use similar logic to what can be seen in this script.

1 Like

Edit: I found the solution. Thank you @Limited_Unique for helping me too.

What do you mean by save? Do you mean like a DataStore? The variable “HasBought” in the script represents whether or not the local player has purchased the item.

1 Like