So I tried making a shop script this time, and it works. The problem is the last 2 items (the Green Sword and Rocket Launcher) is swapped on purchase, meaning when I buy the Green Sword I got a Rocket Launcher and vice versa. And yes, I have FE enabled. If you want to see the explorer I have put it below the scripts
These are the buy scripts:
Local Script:
local buybutton = script.Parent.BuyButton local id = script.Parent.Parent.ItemPressedValue local items = script.Parent.Parent.Items:GetChildren() local player = game.Players.LocalPlayer buybutton.MouseButton1Click:Connect(function() player.leaderstats.Money.Value = player.leaderstats.Money.Value - items[id.Value].Price.Value game:GetService("ReplicatedStorage").Events.BoughtTool:FireServer(id.Value) end)
ServerScript on ServerScriptService
game:GetService("ReplicatedStorage").Events.BoughtTool.OnServerEvent:Connect(function(player, itemId) local items = game:GetService("ReplicatedStorage").Tools:GetChildren() print(player.Name.. "has bought an item of id: ".. itemId) local clone = items[itemId]:Clone() clone.Parent = game:GetService("Players")[player.Name].Backpack end)
The Shop Gui: https://prnt.sc/jpuitl the buying script is the local script
The Item buttons have the name, picture id, price, and item tag on them
The Items: https://prnt.sc/jpui24
If you need more picture I will gladly provide :)
Okay so this code local items = game:GetService("ReplicatedStorage").Tools:GetChildren()
returns it as table like example if you had table
local mytable = {"Handgun","Shotgun","Missle"}
now what the GetChildren() function does is it return the table or "Returns an array of the object's children."
So if itemId = 1
and you use mytable[itemId]
it will get the first one in the table and not by its name.
What I changed is instead of using GetChildren() function I just get the object.And then use itemId as the name if it is that way.
So now items[itemId]
will get it by the name
game:GetService("ReplicatedStorage").Events.BoughtTool.OnServerEvent:Connect(function(player, itemId) local items = game:GetService("ReplicatedStorage").Tools print(player.Name.. "has bought an item of id: ".. itemId) local clone = items[itemId]:Clone() clone.Parent = game:GetService("Players")[player.Name].Backpack end)