Hello!
So, I’m currently working on a crate shop. So basically, I’m wanting to assign different crates to each frame. How could I make this possible? I’m confused about how to call the module to the local script and then assigning items to the frame but making sure that no frame is duplicated. How could I make this possible?
the Example guis contents contain “title”, “price”, “BuyButton” and there’s an IntValue in the “price” textlabel named “Price”
Here’s my code:
-- LOCALSCRIPT in the gui.
-- services
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local MarketplaceService = game:GetService('MarketplaceService')
local Players = game:GetService('Players')
-- constants
local player = Players.LocalPlayer
local PLAYER_DATA = ReplicatedStorage:WaitForChild('PlayerData'):WaitForChild(player.Name)
local REMOTES = ReplicatedStorage:WaitForChild('Remotes')
local MODULES = ReplicatedStorage:WaitForChild('Modules')
local ITEMS = MODULES:WaitForChild('Items')
local EXAMPLE = script.Example
local GUI = script.Parent.Market
local HOLDER = GUI.Holder
-- Set up shop
for i = 1,8 do
local itemFrame = script.Example:Clone()
itemFrame.LayoutOrder = i
itemFrame.Parent = HOLDER
itemFrame.Name = I
-- Stuck on assigning items to the 8 frames.
end
I just realized I didn’t even answer your question properly. So my goal is to have different crates players can open to get a type of currency, I’m trying to grow my scripting knowledge by instead of creating ui for each crate to just have a localscript do it for me. I hope this properly answers your question and hopefully you will be able to help me out.
So I assume what you’re asking is how to add these ‘crate’ frames to the item frames (itemFrame) automatically?
If so, you need to loop through the module script’s table for the set of crates and clone a template frame under your script and edit each dependently.
Make sure that you have a table under module in your ModuleScript that has a list of each crate and its information.
I posted the code for my module script, I’m not sure how to sort through it. Could you help me with that? Or help me figure out an easier way to assign crates to itemframes
And you can loop through the table outside of it like this:
local Module = require(PathToModule)
for Index, Value in pairs(Module.Crates) do
-- Iterate through each of the crate tables.
-- Example usage: Index = 1, 2, ... Value = {CrateName, CrateCost}
end
Hello!
It’s been a while but I just got around to doing this, I’m working on using the same method that you used, but with vehicle names. Here’s what the table looks like:
Here’s my script for assigning the vehicle names and prices to the gui local script
-- services
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local MarketplaceService = game:GetService('MarketplaceService')
local Players = game:GetService('Players')
-- constants
local player = Players.LocalPlayer
local PLAYER_DATA = ReplicatedStorage:WaitForChild('PlayerData'):WaitForChild(player.Name)
local REMOTES = ReplicatedStorage:WaitForChild('Remotes')
local MODULES = ReplicatedStorage:WaitForChild('Modules')
local ITEMS = MODULES:WaitForChild('Items')
local EXAMPLE = script.Example
local GUI = script.Parent.Parent.VehicleStore
local HOLDER = GUI.Holder
-- Setup vehicle frame
local Module = require(ITEMS)
for Index, Value in pairs(Module.Vehicles) do
local itemFrame = script.Example:Clone()
itemFrame.LayoutOrder = Index
itemFrame.Parent = HOLDER
itemFrame.Name = Index
itemFrame.Type.Text = Value
itemFrame.Price.Text = Value
end
Your first problem here is that you’re formatting the vehicles under your ModuleScript in the wrong way for what your use case is.
You are setting Basic Truck as a value in the table, but you’re also setting 5000 as a separate value. There is currently no way to identify which cost is for which type of vehicle!
You need to either combine both of these values into a table like one of these values.
{"Basic Truck", 5000}
{Name = "Basic Truck", Cost = 5000}
["Basic Truck"] = 5000.
After setting it to the first value for the example you can loop through it like this:
for Index, Value in pairs(Module.Vehicles) do
local itemFrame = script.Example:Clone()
local Type = Value[1] -- Or Value.Name for the second example.
local Cost = Value[2] -- Or Value.Cost for the second example.
-- Create the itemFrame and use the Cost value for the Price label.
end
Hello again!
I’ve been working on another script using this method, how would I only get 3 values in the Module? Would I use, would I use a math.random for this?
If you only want to grab and show 3 of the crates you would use a variable as a limiter.
Example usage:
local TimesRan = 0
while true do
if TimesRan >= 5 then
break
end
TimesRan = TimesRan + 1
end
math.random works like Random.new(), generating a pseudo-random number within the numbers you give the functions.
Example usage:
print(math.random()) -- Prints a random number from 0-1
print(math.random(1, 5)) -- Prints either 1, 2, 3, 4, or 5.
print(Random.new():NextInteger(1, 5)) -- Works like above
print(Random.new():NextNumber(1, 5)) -- Works like above too except the decimals are randomized.
*I would rather use Random.new() over math.random() since it allows you to easily generate either an integer or a double, but that’s up to your preference since that might be the only difference.
Hello!
Sorry for the late reply, unfortunately, I do not understand exactly what you’re saying for assigning 3 the 3 crates.
Here’s my code for assigning them!
local Module = require(ITEMS)
for Index, Value in pairs(Module.SellInfo) do
local itemFrame = script.OfferX:Clone()
itemFrame.LayoutOrder = Value.Cost
itemFrame.Parent = HOLDER
itemFrame.Name = Value.Name
itemFrame.Info.Title.Text = itemFrame.Name
itemFrame.Info.Crates.Text = Value.Amount.. " Crates"
itemFrame.Info.Sell.TextLabel.Text = "SELL FOR $"..addComas(tostring(Value.Cost))
itemFrame.Info.Sell.MouseButton1Click:Connect(function()
SOUNDS.Click:Play()
if WAREHOUSE.Vehicle.Value ~= "" and WAREHOUSE.Crates.Value >= 3 then-- Check to see if they own a vehicle, check to see if they have more than 3 crates
REMOTES.SellCrates:FireServer("SellCrates", tonumber(Value.Amount), tonumber(Value.Cost))
else
print("player cannot sell:(")
end
end)
end
I don’t understand if “how would I only get 3 values in the Module” means how you would get 3 values from the module and only show 3 or if you mean to grab 3 random values from your module.