Item selected issue

I am currently making a trail shop GUI that allows you to select the item you want to see, preview it, and then possibly buy it with a buy button. It looks like this at the moment:


The imagelabels are all the trails and the preview is on the right.
Here is the local script located in the shop GUI:

local shopFrame = game.StarterGui.Shop:WaitForChild("Frame")
local itemScroller = script.Parent
local itemPreview = script.Parent.Parent.ItemPreview
local trailsFolder = game.ReplicatedStorage:WaitForChild("Trails")
local ownedTrailsFolder = game.Players.LocalPlayer:WaitForChild("OwnedTrails")
local shopFrame = script.Parent.Parent

function updateShop ()
	
	
	local ownedTrails = ownedTrailsFolder:GetChildren()
	
	for i, trail in pairs(itemsFolder:GetChildren()) do
		local name = trail.Name 
		local price = trail.Price.Value
		local desc = trail.ItemDescription.Value
		local imagepreview = trail.Folder.ImageLabel

		local itemSelection = script.ItemSelection:Clone()

		itemSelection.Parent = itemScroller

		itemSelection.MouseButton1Click:Connect(function()
			itemPreview.Title.Text = name
			itemPreview.TextButton.Text = "Buy for " .. price
			itemPreview.Desc.Text = desc
			itemPreview.PreviewImage.Image = imagepreview.Image
			itemPreview.Visible = true
		end)
		itemPreview.TextButton.MouseButton1Click:Connect(function()
			game.ReplicatedStorage.TrailSelectedRE:FireServer(true, itemSelection)
		end)


	end
end


updateShop()
updateInventory()

ownedTrailsFolder.ChildAdded:Connect(function()
	updateShop(); updateInventory()
end)
ownedTrailsFolder.ChildRemoved:Connect(function()
	updateInventory(); updateShop()
end)

The next script is a normal script in serverscriptservice:

local trails = game.ReplicatedStorage:WaitForChild("Trails")
local Title = game.StarterGui.Shop.Frame.ItemPreview.Title
local itemSelection = game.ReplicatedStorage.Trails:FindFirstChild(Title)


game.ReplicatedStorage.TrailSelectedRE.OnServerEvent:Connect(function(plr, buying, trail)
	
	

	if buying and not plr.OwnedTrails:FindFirstChild(itemSelection.Name) then
		local itemSelection = game.ReplicatedStorage.Trails:FindFirstChild(Title)
		print("Message Recieved")
		
		local price = itemSelection.Price.Value
		local coins = plr.leaderstats.Coins

		if price <= coins.Value then

			coins.Value -= price


			itemSelection:Clone().Parent = plr.OwnedTrails
		end
	

	elseif not buying and plr.OwnedTrails:FindFirstChild(itemSelection.Name) then

		local char = plr.Character

		if not char or not char:FindFirstChild("HumanoidRootPart") then return end

		for i, child in pairs(char.HumanoidRootPart:GetChildren()) do


			if child:IsA("Trail") then child:Destroy() end
		end


		local newTrail = itemSelection:Clone()

		newTrail.Attachment0 = char.HumanoidRootPart.TrailTop
		newTrail.Attachment1 = char.HumanoidRootPart.TrailBottom

		newTrail.Parent = char.HumanoidRootPart
	end
end)

I identify the item selected to be bought with the variable ‘itemSelection.’ The issue is, I am getting six messages sent from the shop local script trying to buy all of the trails. I attempted to fix this with the itemSelection variable in the serverscript by only buying the item the player selected. The thing is, I do not think it worked because I made a print(ItemSelection) below the onservereventconnect part of the serverscript and it did not print anything which means it is not identifying an itemSelection. Is there a way to change my scripts so it is only sending a message to the serverscript to buy only the itemSelection or filter out the itemSelection in the serverscript.

Note: If you find any issues with my script that might cause problems in the future, please communicate them with me. Another note is that I did not include some parts of the script for length reasons. If you want to see them, just tell me. If you have any questions, just ask.

2 Likes

when the itempreview is clicked it fires the buy for all of them because you looped through them.

local shopFrame = game.StarterGui.Shop:WaitForChild("Frame")
local itemScroller = script.Parent
local itemPreview = script.Parent.Parent.ItemPreview
local trailsFolder = game.ReplicatedStorage:WaitForChild("Trails")
local ownedTrailsFolder = game.Players.LocalPlayer:WaitForChild("OwnedTrails")
local shopFrame = script.Parent.Parent

local currentlySelected;
function updateShop ()
	
	
	local ownedTrails = ownedTrailsFolder:GetChildren()
	
	for i, trail in pairs(itemsFolder:GetChildren()) do
		local name = trail.Name 
		local price = trail.Price.Value
		local desc = trail.ItemDescription.Value
		local imagepreview = trail.Folder.ImageLabel

		local itemSelection = script.ItemSelection:Clone()

		itemSelection.Parent = itemScroller

		itemSelection.MouseButton1Click:Connect(function()
            currentlySelected = **put what is going to be sent to the server here**
			itemPreview.Title.Text = name
			itemPreview.TextButton.Text = "Buy for " .. price
			itemPreview.Desc.Text = desc
			itemPreview.PreviewImage.Image = imagepreview.Image
			itemPreview.Visible = true
        
		end)
	end
end
		itemPreview.TextButton.MouseButton1Click:Connect(function()
                        if currentlySelected then
			       game.ReplicatedStorage.TrailSelectedRE:FireServer(true, itemSelection)
                       end
		end)

updateShop()
updateInventory()

you can fix that problem like this

1 Like

Wait, disregard what I said. 30

Thank you for helping. I am getting an error in the parenthesis of

 game.ReplicatedStorage.TrailSelectedRE:FireServer(true, itemSelection)

because itemSelection is not identified anymore due to the function being out of the updateShop function. How should I go about this?

Oh, my bad, I forgot to change it. game.ReplicatedStorage.TrailSelectedRE:FireServer(true, currentlySelected)

1 Like

Good news and bad news. Good news is that it is only sending one message now. Bad news is I am now getting attempt to index nil with ‘Name’ error in line 98 of the serverscript which is:

if buying and not plr.OwnedTrails:FindFirstChild(itemSelection.Name) then

Do I change all “ItemSelection” variables to currentlySelected?

This is because you are sending an instance through the remote event. Instead why don’t you send the trail through the remote event?

Thank you for helping me. I set your post as a solution. If you have time, could you help me with one more thing though? When the player purchases a trail, it is duplicating all the trails in the shop GUI instead of just keeping the normal 6. Why is it doing this?
Here is what I am talking about
Before:


After:

As you can see, it is just duplicating all the items. How do I fix this in my updateshop function?

Does the local script run more than once or do you call the updateShop() function more than once?


updateShop()
updateInventory()

ownedTrailsFolder.ChildAdded:Connect(function()
	updateShop(); updateInventory()
end)
ownedTrailsFolder.ChildRemoved:Connect(function()
	updateInventory(); updateShop()
end)


trailsFolder.ChildAdded:Connect(updateShop)
trailsFolder.ChildRemoved:Connect(updateShop)

So I call it when a child is added to the ownedTrailsFolder and removed. I tried adding this script before the updateshop function:

for i, child in pairs(shopFrame.Trails:GetChildren()) do
		if child:IsA("ImageLabel") then child:Destroy() end
	end

But it did not work.

Do you happen to know what to add or change?

Sorry I didn’t answer sooner, I didn’t see the notification. I can’t exactly see where the problem could be. Is there somewhere else in the script where you call the updateShop function? and if so why do you need to run the updateshop function more than once?

I don’t know why I included updateshop to the childadded function because it makes no sense since nothing would be changing in the shop. Thank you for helping. If you have time, I have one more issue about my trail GUI. So the shop GUI works and all that but the inventory gui is not the same. It is supposed to look like this (do not mind the poor gui skills):


Each frame represents one of the trails that the player purchased. Instead, when in the game, it looks like this:

I know there are items in the inventory frame because it shows in the explorer and I also know the items frames visibility is = true. This means that it has something to do with the inventory script. Do you mind checking what is going wrong?