How would i go about fixing and making this system?

Hello!

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

Please help!

1 Like

You’ll need to call your CreatePotionPurchaseFrame function, or indeed nothing will happen.

1 Like

oh yeah lol, forgot about that xd

Nevermind, its still broke with this error:

Is this script running on the server or the client? Players.LocalPlayer is always nil on the server, which would explain why you cannot index it.

1 Like

This is a module script. And im using :WaitForChild(“”) So the client i assume

ModuleScripts can run on both the server and the client depending on what requires it. Where is this module being required?

The module is being required on the server

task.wait(3)

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")["Taps Potion"], 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()
	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

task.wait(3)

local MerchantHandler = require(game:GetService("ReplicatedStorage"):FindFirstChild("Merchant").MerchantHandler)

MerchantHandler.CreatePotionPurchaseFrame()

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.

1 Like

Thanks! But i still get the “Requested module failed to load” error

Could you post the error message here?

1 Like

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”

Nevermind, the message disappeared. i just made some changes. Thanks for your help!

1 Like

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")
1 Like

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