Issue with Trail Shop (Player can keep buying trail) SOLVED

I made a trail shop a bit ago, and today while making some changes to it, I noticed after a player buys a trail, the text changes to equip and it allows the player to wear the trail, but it charges them the price again. This probably has something to do with the Button still being connected to the buy function. I’m not sure what to do about it, because if I set a new connect function, the old one is still there

This is my code for the shop

local function SetNames()
	for Index, TrailOwned in ipairs(Player.Trails:GetChildren()) do
		if TrailOwned.Value then
			local TrailFrame = TrailFrames[TrailOwned.Name]
			local TrailButton = TrailFrame.TrailButton
			local TrailName = TrailButton.TrailName
			
			TrailName.Text = "Equip"
		end
	end
end

local function CheckForChanges()
	--Setting up Property Changed Signal on all Trail Values
	--Will Set the text of a button to equip if the player already owns the trail
	for Index, Trail in ipairs(Player.Trails:GetChildren()) do
		if Trail:IsA("BoolValue") then
			Trail:GetPropertyChangedSignal("Value"):Connect(SetNames)
		end
	end
end

local function SetFunctions()
	for Index, TrailOwned in ipairs(Player.Trails:GetChildren()) do
		task.wait(0.1)
		local TrailFrame = TrailFrames[TrailOwned.Name]
		local TrailButton = TrailFrame.TrailButton
		local TrailName = TrailButton.TrailName

		if TrailName.Text == "Equip" then
			TrailButton.MouseButton1Down:Connect(function()
				EquipEvent:FireServer(TrailFrame.Name)
				ClickSound:Play()
			end)
			
		elseif TrailName.Text == "Buy" then
			TrailButton.MouseButton1Down:Connect(function()
				if Player.leaderstats.Diamonds.Value >= TrailPrices[TrailFrame.Name] then
					BuyEvent:FireServer(TrailFrame.Name, TrailPrices[TrailFrame.Name])
					PurchaseSound:Play()
				else
					PurchaseFailedSound:Play()
				end
			end)
		end
	end
end

It basically just checks for a change in a folder attached to the player, and if there is one, it will set the name to equip.

Need help with getting rid of the player being able to buy it again while trying to equip the ttrail

Actually this was a dumb question, because I could have just checked in the server script handling the purchase if the player already owned the trail

Solution

local function BuyTrail(Player, TrailName, TrailPrice)
	if Player.leaderstats.Diamonds.Value >= TrailPrice then
		if Player.Trails:WaitForChild(TrailName).Value == true then
			return
		end
		
		Player.leaderstats.Diamonds.Value -= TrailPrice
		
		Player.Trails[TrailName].Value = true
		TrailModule.AddTrail(Player, TrailName)
	end
end

You should also just check on the client and change the UI for user experience reasons.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.