Gear Shop Script Not Working

I have a few scripts for my gear shop.

One of them is a Module script that separates the prices on each gear for players who have Roblox Premium and players who don’t.

The other module script (that requires the first module script) determines whether the player (depending on their membership status) has enough money to buy the gear. If they do, it gear clones into their backpack. If they don’t, nothing happens.

The problem is that they don’t work when the player clicks the button on the shop to buy the select tool.

Here are the scripts:

-- First Module Script, located in ServerStorage
-- Each Tool has its own button in the shop
local ToolSettings = {}
local Tools = game.ServerStorage.Tools

ToolSettings["Gravity Coil"] = {
	Tool = Tools.GravityCoil;
	Price = 50;
	PremiumPrice = 25;
}

ToolSettings["DualGravityCoil"] = {
	Tool = Tools.DualGravityCoil;
	Price = 150;
	PremiumPrice = 75;
}

ToolSettings["Regen Coil"] = {
	Tool = Tools.RegenCoil;
	Price = 50;
	PremiumPrice = 25;
}

ToolSettings["DualRegenCoil"] = {
	Tool = Tools.DualRegenCoil;
	Price = 150;
	PremiumPrice = 75;
}

ToolSettings["SpeedCoil"] = {
	Tool = Tools.SpeedCoil;
	Price = 50;
	PremiumPrice = 25;
}

ToolSettings["DualSpeedCoil"] = {
	Tool = Tools.DualSpeedCoil;
	Price = 150;
	PremiumPrice = 75;
}

ToolSettings["GreenLaserGun"] = {
	Tool = Tools.GreenHyperLaser;
	Price = 150;
	PremiumPrice = 75;
}

ToolSettings["Ro-Orb"] = {
	Tool = Tools["Ro-Orb"];
	Price = 200;
	PremiumPrice = 100;
}

ToolSettings["Railgun"] = {
	Tool = Tools.Railgun;
	Price = 350;
	PremiumPrice = 175;
}

ToolSettings["Railgun2"] = {
	Tool = Tools.Railgun2;
	Price = 750;
	PremiumPrice = 375;
}

ToolSettings["Railgun3"] = {
	Tool = Tools.Railgun3;
	Price = 2000;
	PremiumPrice = 1000;
}

ToolSettings["Shotgun"] = {
	Tool = Tools.Shotgun;
	Price = 200;
	PremiumPrice = 100;
}

ToolSettings["Shotgun2"] = {
	Tool = Tools.Shotgun2;
	Price = 500;
	PremiumPrice = 250;
}

ToolSettings["Shotgun3"] = {
	Tool = Tools.Shotgun3;
	Price = 1000;
	PremiumPrice = 500;
}


ToolSettings["Sniper"] = {
	Tool = Tools.Sniper;
	Price = 250;
	PremiumPrice = 125;
}

ToolSettings["Sniper2"] = {
	Tool = Tools.Sniper2;
	Price = 750;
	PremiumPrice = 375;
}

ToolSettings["Sniper3"] = {
	Tool = Tools.Sniper3;
	Price = 2000;
	PremiumPrice = 1000;
}

ToolSettings["Crossbow"] = {
	Tool = Tools.Crossbow;
	Price = 100;
	PremiumPrice = 50;
}

ToolSettings["CrossBow2"] = {
	Tool = Tools.Crossbow2;
	Price = 500;
	PremiumPrice = 250;
}

ToolSettings["CrossBow3"] = {
	Tool = Tools.Crossbow3;
	Price = 1500;
	PremiumPrice = 750;
}

ToolSettings["Grenade Launcher"] = {
	Tool = Tools["Grenade Launcher"];
	Price = 150;
	PremiumPrice = 75;
}

ToolSettings["Grenade Launcher2"] = {
	Tool = Tools["Grenade Launcher2"];
	Price = 500;
	PremiumPrice = 250;
}

ToolSettings["Grenade Launcher3"] = {
	Tool = Tools["Grenade Launcher3"];
	Price = 1250;
	PremiumPrice = 625;
}



return ToolSettings
-- Second Module Script
-- Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")

-- Other Variables
local Tools = ServerStorage.Tools
local Membership = Enum.MembershipType
local None = Membership.None
local Premium = Membership.Premium
local ToolSettings = require(game.ServerStorage.ToolConfig)

ReplicatedStorage.BuyEvent.OnServerEvent:Connect(function(player, toolname)


		local Setting = ToolSettings[toolname]

		if not Setting then return end

		local Price = (Membership == Premium and Setting.PremiumPrice) or Setting.Price

		local Cash = player.leaderstats.Cash

		local Backpack = player:WaitForChild("Backpack")

		if Cash.Value >= Price then
			Cash.Value -= Price
			Setting.Tool:Clone().Parent = Backpack
		end

end)

What am I doing wrong?

  1. What does the local script look like that is firing the event?
  2. You could just remove all the premium prices and do this to prevent the hassle of typing all of that
local Price = (Membership == Premium and Setting.Price/2) or Setting.Price
1 Like

I’m dumb. You’re definitely right. That would’ve saved me a lot of time. :man_facepalming:

script.Parent.MouseButton1Click:Connect(function(player, toolname)
	game.ReplicatedStorage.BuyEvent:FireServer()
end)

You’re not passing over the toolName to the server. So Setting[toolname] isn’t valid, therefore it returns.

Edit with more info:
How I would approach this is name the Button the toolName for what you’re trying to buy. If all of the buttons are parented under the same frame, you can just loop through it and have functionality like this:

local frame = ... -- wherever your buttons are parented to
for _, button in pairs(frame:GetChildren()) do
    if button:IsA("GuiButton") then
        button.Activated:Connect(function()
            game.ReplicatedStorage.BuyEvent:FireServer(button.Name)
        end)
    end
end

And that should handle all the buttons in those few lines of code, assuming it was setup properly.

1 Like

Change it to this:

script.Parent.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.BuyEvent:FireServer(toolname)
end)
1 Like

Are you calling the function from a local script?

If so, everything that happens within the function only happens in on the client’s side and not the server’s side. Change it so that another server script calls the function or just put the function in another script that is located in server script storage.

Remember, when you are calling functions from module scripts, they can do stuff on the client’s side (when called from a local script) and do server stuff (when called from a server script)

Edit: Nevermind, I just realized he is using remote events. Remote events should be fine when working with module scripts.

Your script didn’t work, unfortunately.

Apparently, “toolname” is an unknown global.

Maybe its because there’s no parameter?

Can you show me the hierarchy of your shop GUI?

1 Like

image

If you name the button the same as the gear that the button relates to, you can just do this:

script.Parent.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.BuyEvent:FireServer(script.Parent.Name)
end)
1 Like

Thanks! That worked.

But now the Premium Price discount won’t apply to the gears :confused:

You need to check the premium status of the player.

local Price = (player.MembershipType == Premium and Setting.PremiumPrice) or Setting.Price

Yes!

It worked! Thanks a lot. :slight_smile:

1 Like