Need help accessing Library information

I’m kind of stuck how to access the library information when the button is touched. The library is so named because it will be mapped to the players playerstats, Button[playerstats.Name] or something like that so that way I can easily update the playerstats values later on in the script. I hit a snag when I don’t know how to access the library using the name of the button “FireButton” for example to correspond to ButtonFire in the library. Any help would be appreciated.


local ButtonDataMapping = require(game:GetService("ReplicatedStorage").ButtonDataModuleScript)
local CollectionService = game:GetService("CollectionService")
local taggedParts = CollectionService:GetTagged("TaggedButtons")
local gameButtons = game.Workspace.Buttons
local waitTime = 1

-- separate the elemental buttons for resetting values after cost analysis
local elementalButtons = {

	FireButton = gameButtons.FireButton,
	FireModButton = gameButtons.FireModButton,
	WaterButton = gameButtons.WaterPowerButton,
	WaterModButton = gameButtons.WaterPureButton,
	EarthButton = gameButtons.EarthPowerButton,
	EarthModButton = gameButtons.EarthEnrichmentButton,
	AirButton = gameButtons.AirPowerButton,
	AirModButton = gameButtons.AirQualityButton,

}
-- Tagged buttons are scripted together --
for _, tagged in pairs(taggedParts)do -- get all buttons that are tagged
	
	local db = true
	tagged.Touched:Connect(function(otherPart)
		local player = game:GetService("Players"):GetPlayerFromCharacter(otherPart.Parent)
		
		if player and db then
			db = false
			local buttonTouched = tagged.Parent
			
			-- checking whether the tagged part is elemental or not
			-- this is because elemental buttons don't reset other elemental buttons
			local isElemental = false
			local key = nil -- The key to access the ButtonDataMapping
			for butName, butValue in pairs(elementalButtons) do
				if buttonTouched.Name == butValue.Name then
					isElemental = true
					break
				end
			end
			
			-- get button cost here
			
			
			
			--[[
			
			if isElemental then
				print("Button is Elemental "..buttonTouched.Name)
			else
				print("Button is not Elemental "..buttonTouched.Name)
			end
			
			when the player touches the button
			first thing is to check if they have enough currency
			this means getting the button cost first from the module script
			using the button name as reference
			check button name against module?
			get the players energy
			if the player has enough energy then continue
			the cost increases each time the player buys the button
			this is where the rate of change comes in
			new button cost = button base cost * rate of change
			need to loop through the players available currency each time the player
			buys the button
			remove the cost
			continue buying the button until the players currency < new cost
			
			after that set the players currency to 0
			and any other currencies the button needs to reset - if isElemental = true
			
			
			
			]]
					
			
			task.wait(0.05)
			db = true
		end
	end)
end

Module Library Information

-- Version 1.0
local ButtonDataMapping = {
	ButtonFire = {
		ButtonName = "FireButton",
		ButtonCost = 1000,
		ButtonValue = 0.25,
		RequiredCurrency = "Energy",
		RateOfCostChange = 4,
		DisplayCost = game.Workspace.InformationBoards.Fire.FireDisplay.SurfaceGui.Cost,
		DisplayCurrentRank = game.Workspace.InformationBoards.Fire.FireDisplay.SurfaceGui.CurrentRank,
		DisplayPerRank = game.Workspace.InformationBoards.Fire.FireDisplay.SurfaceGui.PerRank,
	},
	
...
-- Further library data...
2 Likes

Are you / how are you returning the dictionary “ButtonDataMapping” in the module? Here’s an example of how you could go about doing so:

Module:

local Public = {}

Public.Data = {
	['ButtonName'] = {
		Name = 'Example',
		Value = 5,
	}
}

return Public

LocalScript

local Module = require(ModulePath)

for i,v in ipairs(Buttons) do
	v.Activated:Connect(function()
		local Data = Module.Data[v.Name]
	end)
end

Hope this helps! :slight_smile:

2 Likes

So it’s not possible to access it directly instead of looping through the whole library? Also do I need to put every button name in [ ] or is it superfluous?

2 Likes

You wouldn’t be looping through the whole library, you’d only be accessing data from the library when a button is pressed. You don’t have to put them in square brackets, you could write it without the brackets and quotations but I just typically do for making it look cleaner.

1 Like

Umm, there’s 3 scripts accessing this module. one for the button touched, one for the displaying the cost, etc and another to light up the button when the player has the right amount of currency.

I was hoping to find a way to access it directly to speed up response time.

perhaps I can modify the library to help this approach? having [FireButton] = { ButtonName = “FireButton”, …} look too stupid?

1 Like

To access the data this is the only line you need (besides requiring the module), just replace Data with the name of your table and replace v.Name with the name of your key.

1 Like

Rename FireButton in dictionary to be ButtonFire. Pretty simple workaround.

You don’t really need 3 separate scripts for handling that – by returning the overall data for specific button like in my example, you could then check the cost and light up the button if they have enough. I’d recommend capturing Touched on the client and then when they try to purchase, verify they have enough currency on the server using a RemoteEvent or RemoteFunction – never trust the client.

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