I am trying to create a potion merchant. Where you enter the potion name, time, icon, maxpurchaseable and amountpurchased. However, When creating a template. nothing happens:
local MerchantHandler = {}
local Player = game.Players.LocalPlayer
local PlayerBoosts = Player:WaitForChild("Boosts")
MerchantHandler.PotionPurchaseCooldown = 3600 -- (3600 = 1 Hour), (86400 = 1 Day)
local Items = {
{Potion = PlayerBoosts["Gem Potion"], Time = 30, Image = "rbxassetid://15434984615", Title = "Gem Potion", MaxPurchase = 1, Purchased = 0},
{Potion = PlayerBoosts["Taps Potion"], Time = 60, Image = "rbxassetid://15434984615", Title = "Taps Potion", MaxPurchase = 2, Purchased = 0},
{Potion = PlayerBoosts["Extra Lucky"], Time = 90, Image = "rbxassetid://15434984615", Title = "Luck Potion", MaxPurchase = 3, Purchased = 0},
}
local function CreatePotionPurchaseFrame()
local Template = script.Parent:WaitForChild("Potion1"):Clone()
local gui = Player.PlayerGui:WaitForChild("Main"):WaitForChild("PotionMerchant")
Template.Name = Items[index].Title
Template.Icon.Image = Items[index].Image
Template.PotionName.Text = Items[index].Title
Template.Purchased.Text = Items[index].Purchased.."/"..Items[index].MaxPurchase
Template.Parent = gui:WaitForChild("Main"):WaitForChild("Container")
end
return MerchantHandler
That is the problem then, it must be run on the client to use the LocalPlayer. Server scripts do not have a local player because they handle all the players on the server, whereas clients only handle the one running the game instance, thus being called the LocalPlayer. To get this to work on the server, you’d need to define the player first, whether that be through events or whatnot.
Try requiring it in a local script in StarterPlayerScripts, then it should work assuming everything else is in place. If this works please make sure to mark as solved.
Sure! and i made some changes to the code so it actually works:
local MerchantHandler = {}
local Player = game:GetService("Players").LocalPlayer
MerchantHandler.PotionPurchaseCooldown = 3600 -- (3600 = 1 Hour), (86400 = 1 Day)
local Items = {
{Potion = Player:WaitForChild("Boosts")["Gem Potion"], Time = 30, Image = "rbxassetid://15434984615", Title = "Gem Potion", MaxPurchase = 1, Purchased = 0},
{Potion = Player:WaitForChild("Boosts")["Super Lucky"], Time = 60, Image = "rbxassetid://15434984615", Title = "Taps Potion", MaxPurchase = 2, Purchased = 0},
{Potion = Player:WaitForChild("Boosts")["Extra Lucky"], Time = 90, Image = "rbxassetid://15434984615", Title = "Luck Potion", MaxPurchase = 3, Purchased = 0},
}
function MerchantHandler.CreatePotionPurchaseFrame(name)
local Template = script.Parent:WaitForChild("Potion1"):Clone()
local gui = Player.PlayerGui:WaitForChild("Main"):WaitForChild("PotionMerchant")
Template.Name = Items[name].Title
Template.Icon.Image = Items[name].Image
Template.PotionName.Text = Items[name].Title
Template.Purchased.Text = Items[name].Purchased.."/"..Items[name].MaxPurchase
Template.Parent = gui:WaitForChild("Main"):WaitForChild("Container")
end
return MerchantHandler
local MerchantHandler = require(game:GetService("ReplicatedStorage"):FindFirstChild("Merchant").MerchantHandler)
MerchantHandler.CreatePotionPurchaseFrame(???)
“Requested module experienced an error while loading”
You would need to replace the ??? with a number, since the Items table is an array, not a dictionary. Also, as for the error, please make sure the ModuleScript is parented to the correct instance, and that the "Gem Potion", "Super Lucky" and "Extra Lucky" instances exist within the Boosts folder at the time that they are indexed for. If they don’t exist within the instance when they’re indexed for, it’ll error because it’s trying to find something that doesn’t exist. Instead, replace that with
Player:WaitForChild("Boosts"):WaitForChild("Name of the instance goes here")