Shop gives all the items to the player?

What I want to achieve is that I want the shop to give 1 object to the player and remove the points of how much it cost.

The shop works. When the player clicks the text button, it removes the cost from the points that player has and gives them an item. The problem is that it gives them ALL of the items.

Code

Local Script from the ScreenGui

local folder = game.ReplicatedStorage.Tools
local gravitycoil = script.Parent.MainFrame.ScrollingFrame.GravityCoil
local speedcoil = script.Parent.MainFrame.ScrollingFrame.SpeedCoil
local sword = script.Parent.MainFrame.ScrollingFrame.ClassicSword

for _, tool in pairs(folder:GetChildren()) do
	gravitycoil.MouseButton1Click:Connect(function()
		local result = game.ReplicatedStorage.BuyItem:InvokeServer(tool.Name)

		if result == true then
			gravitycoil.BackgroundColor3 = Color3.fromRGB(55, 255, 85)
			gravitycoil.Text = "Successfully purchased item!"
			wait(1)
			gravitycoil.Text = "Gravity Coil - 50 Points"
		else
			gravitycoil.BackgroundColor3 = Color3.fromRGB(255, 60, 60)
			gravitycoil.Text = "Insufficent Funds!"
			wait(1)
			gravitycoil.Text = "Speed Coil - 50 Points"
		end

		wait(1)

		gravitycoil.BackgroundColor3 = Color3.fromRGB(255, 255, 255)

	end)
end


for _, tool in pairs(folder:GetChildren()) do
	speedcoil.MouseButton1Click:Connect(function()
		local result = game.ReplicatedStorage.BuyItem:InvokeServer(tool.Name)

		if result == true then
			speedcoil.BackgroundColor3 = Color3.fromRGB(55, 255, 85)
			speedcoil.Text = "Successfully purchased item!"
			wait(1)
			speedcoil.Text = "Speed Coil - 50 Points"
		else
			speedcoil.BackgroundColor3 = Color3.fromRGB(255, 60, 60)
			speedcoil.Text = "Insufficent Funds!"
			wait(1)
			speedcoil.Text = "Speed Coil - 50 Points"
		end

		wait(1)

		speedcoil.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
	end)
end

for _, tool in pairs(folder:GetChildren()) do
	sword.MouseButton1Click:Connect(function()
		local result = game.ReplicatedStorage.BuyItem:InvokeServer(tool.Name)

		if result == true then
			sword.BackgroundColor3 = Color3.fromRGB(55, 255, 85)
			sword.Text = "Successfully purchased item!"
			wait(1)
			sword.Text = "Sword - 500 Points"
		else
			sword.BackgroundColor3 = Color3.fromRGB(255, 60, 60)
			sword.Text = "Insufficent Funds!"
			wait(1)
			sword.Text = "Sword - 500 Points"
		end

		wait(1)

		sword.BackgroundColor3 = Color3.fromRGB(255, 255, 255)

	end)
end

Server Script Service

game.ReplicatedStorage.BuyItem.OnServerInvoke = function(player,toolName)

	local tool = game.ReplicatedStorage.Tools:FindFirstChild(toolName)

	if tool:FindFirstChild("Cost") then
		
		if tool and not player.StarterGear:FindFirstChild(toolName) then

			if tool.Cost.Value <= player.leaderstats.Points.Value then
				
				player.leaderstats.Points.Value = player.leaderstats.Points.Value - tool.Cost.Value
				
				local newTool = tool:Clone()
				newTool.Parent = player.Backpack
				
				local newTool = tool:Clone()
				newTool.Parent = player.StarterGear
				
				print(player.DisplayName.."has bought "..toolName)
				
				return true
			else
				return false
			end
		else
			return false
		end
	else
		return false
	end
end

There is an IntValue inside each tool collecting the cost and removing from the leaderstats. There is also a remoteFunction in the ReplicatedStorage Section.

Can anybody help? Why does it give all the items?

1 Like

Anybody know or see an error? I didn’t see any error in the output.

1 Like

Local Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local folder = ReplicatedStorage.Tools
local Ui = script.Parent
local ScrollingFrame = Ui.MainFrame.ScrollingFrame
local gravitycoil = ScrollingFrame.GravityCoil
local speedcoil = ScrollingFrame.SpeedCoil
local sword = ScrollingFrame.ClassicSword

local RemoteFunction = ReplicatedStorage.BuyItem
local GRAVITYCOIL = folder["Gravity Coil"] -- Replace this variable by the Gravity Coil Tool in your Tools folder
local SPEEDCOIL = folder["Speed Coil"] -- Replace this variable by the Speed Coil Tool in your Tools folder
local SWORD = folder["Classic Sword"] -- Replace this variable by the Classic Sword Tool in your Tools folder

gravitycoil.MouseButton1Click:Connect(function()
	local result = RemoteFunction:InvokeServer(GRAVITYCOIL.Name)

	if result == true then
		gravitycoil.BackgroundColor3 = Color3.fromRGB(55, 255, 85)
		gravitycoil.Text = "Successfully purchased item!"
		wait(1)
		gravitycoil.Text = "Gravity Coil - 50 Points"
	else
		gravitycoil.BackgroundColor3 = Color3.fromRGB(255, 60, 60)
		gravitycoil.Text = "Insufficent Funds!"
		wait(1)
		gravitycoil.Text = "Speed Coil - 50 Points"
	end

	wait(1)

	gravitycoil.BackgroundColor3 = Color3.fromRGB(255, 255, 255)

end)




speedcoil.MouseButton1Click:Connect(function()
	local result = RemoteFunction:InvokeServer(SPEEDCOIL.Name)

	if result == true then
		speedcoil.BackgroundColor3 = Color3.fromRGB(55, 255, 85)
		speedcoil.Text = "Successfully purchased item!"
		wait(1)
		speedcoil.Text = "Speed Coil - 50 Points"
	else
		speedcoil.BackgroundColor3 = Color3.fromRGB(255, 60, 60)
		speedcoil.Text = "Insufficent Funds!"
		wait(1)
		speedcoil.Text = "Speed Coil - 50 Points"
	end

	wait(1)

	speedcoil.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
end)


sword.MouseButton1Click:Connect(function()
	local result = RemoteFunction:InvokeServer(SWORD.Name)

	if result == true then
		sword.BackgroundColor3 = Color3.fromRGB(55, 255, 85)
		sword.Text = "Successfully purchased item!"
		wait(1)
		sword.Text = "Sword - 500 Points"
	else
		sword.BackgroundColor3 = Color3.fromRGB(255, 60, 60)
		sword.Text = "Insufficent Funds!"
		wait(1)
		sword.Text = "Sword - 500 Points"
	end

	wait(1)

	sword.BackgroundColor3 = Color3.fromRGB(255, 255, 255)

end)

Hope it helps you!

1 Like

Thank you so much! Helped and worked!

1 Like