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?