Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can I fix this shop script that I made?

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 :)

1 comment

0

The printing id is correct but it gave me the wrong item :)

1 answer

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)

3 comments

0

Let me know if that is what you meant?

0

So, I tried your script and it didn't work with the error of "8 is not a valid member of Tools". My fix was taking the sword and rocket launcher out of the folder and putting it back in and weirdly, it worked. Thanks for helping anyway :)

0

It work weirdly because the positions of the two in the folder changed.so if itemId is 1 and first one in folder is rocket launcher then you will get rocket launcher but if more items get added then you might get the same problem because the positions will change.Look at this script for refence of why that happens.

This discussion is preserved as it was. New answers, comments, and votes are closed.