Buy button in gear shop gui not changing text when bought

Hi there, I have a shop gui that allows you to buy gears using a coins value but the problem is, is that the buy button I have, won’t make the buy button inactive and change to the text like “OWNED”. I’ve tried making it so that if the player has a tool in their inventory or startergear, it makes the buy button inactive.

local ToolName = "BloxyCola" -- tool name
local text = script.Parent.Parent.GEARPRICE
local button = script.Parent.Parent.BUTTON
local Backpack = game.Players.LocalPlayer.Backpack
local StarterGear = game.Players.LocalPlayer.StarterGear


game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		if Backpack:FindFirstChild(ToolName) or StarterGear:FindFirstChild(ToolName)then --checks to see if they own it
			text.Text = "OWNED" 
			text.TextColor3 = Color3.new(0.333333, 0.333333, 1) -- blue text
			button.Visible = false
			button.Active = false
		elseif Player.Coins.Value <= 100 then -- checks to see if they have enough money
			text.TextColor3 = Color3.new(0,255,0) --green text
		elseif Player.Coins.Value >= 100 then-- if they dont have enough money
			text.TextColor3 = Color3.new(1, 0, 0) --red text
		end
	end)
end)

script.Parent.MouseButton1Click:Connect(function(plr) --when they click the button
	if Backpack:FindFirstChild(ToolName) or StarterGear:FindFirstChild(ToolName)then --checks to see if they own it
		text.Text = "ALREADY OWNED"
		text.TextColor3 = Color3.new(0.333333, 0.333333, 1)
	else
		game.ReplicatedStorage.ToolEvents.ColaEvent:FireServer()  -- fire event here
	end
end)
2 Likes

Can you try replacing these 2 lines with:

local Backpack = game.Players.LocalPlayer:WaitForChild("Backpack")
local StarterGear = game.Players.LocalPlayer:WaitForChild("StarterGear")
1 Like

still doesn’t work. i replaced the first locals and it didn’t work. there’s no text changing in the buy button

Okay then. Can you add a print statement under every if block:
For example:

if Backpack:FindFirstChild(ToolName) or StarterGear:FindFirstChild(ToolName)then --checks to see if they own it
            print("1")
			text.Text = "OWNED" 
			text.TextColor3 = Color3.new(0.333333, 0.333333, 1) -- blue text
			button.Visible = false
			button.Active = false
		elseif Player.Coins.Value <= 100 then -- checks to see if they have enough money
            print("2")
			text.TextColor3 = Color3.new(0,255,0) --green text
		elseif Player.Coins.Value >= 100 then-- if they dont have enough money
            print("3")
			text.TextColor3 = Color3.new(1, 0, 0) --red text
		end

This will be helpful to find out where the problem is. And by the way, this condition isn’t correct:

elseif Player.Coins.Value <= 100 then -- checks to see if they have enough money

Because the player should have more than 100 if he has enough money,
Same thing with the condition under it (if they don’t have enough money).

How do I fix the condition? I’m new to lua sorry but I get what you mean

Okay try this code here: Player.CharacterAdded:Connect(function(Character) [CODE HERE] end)

repeat
    wait()
    Backpack = game.Players.LocalPlayer:WaitForChild("Backpack")
    StarterGear = game.Players.LocalPlayer:WaitForChild("StarterGear")
until Backpack and StarterGear

if Backpack:FindFirstChild(ToolName) or StarterGear:FindFirstChild(ToolName)then --checks to see if they own it
    print("1")
	text.Text = "OWNED" 
	text.TextColor3 = Color3.new(0.333333, 0.333333, 1) -- blue text
	button.Visible = false
	button.Active = false
elseif Player.Coins.Value >= 100 then -- checks to see if they have enough money
    print("2")
	text.TextColor3 = Color3.new(0,255,0) --green text
else -- if they dont have enough money
    print("3")
	text.TextColor3 = Color3.new(1, 0, 0) --red text
end

nope still didnt work, no output from the prints

Do the tool names match correctly?

Yep, I can buy a tool from a shop and get it after a character reset

Yes, but is the tool name actually also “BloxyCola”?
It has to be matched entirely “bloxycola” and “bloxyCola” would both result in the user not owning it.

Yes it is named BloxyCola. Caps sensitive

Is it a LocalScript or a Script?

localscript inside the buy button

I don’t think PlayerAdded is the best approach to this.
Since it is a LocalScript you could do this:

local players = game:GetService("Players")

local player = Players.LocalPlayer
local toolName = "BloxyCola"

player.CharacterAdded:Connect(function(character)
	if player.Backpack:FindFirstChild(toolName) then
		print("Player has tool!")
	else
		print("Player doesn't have tool.")
	end
end)