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

How to fix this script so that the player can only buy it once?


Annyeong!Hello!

So I have a shop gui,then there is a buy button,what i want it to do is that i want the player to buy a tool,then the player will not be able to buy it again.It will cost the in game currency,so i do not want it to be in a games form.However it doesn't work.I don't know what is wrong but i tried :FindFirstChild and still did not work

There was also NO output error


Local Script:

local plr = game.Players.LocalPlayer
local Cloud = game.ReplicatedStorage["Flying Cloud"]
local button = script.Parent


button.MouseButton1Click:Connect(function()
	if plr.leaderstats.Scikoins.Value == 100 or plr.leaderstats.Scikoins.Value >= 100 then
plr.leaderstats.Scikoins.Value = plr.leaderstats.Scikoins.Value - 100		
Cloud:Clone().Parent = plr.Backpack
		wait(2)
		warn(plr.Name.."has bought a cloud")
	    else
		warn(plr.Name.."has not enough scikoins")
		if plr.Backpack:FindFirstChild("Flying Cloud") then
			warn(plr.Name.."already has a cloud")
		end
	end
end)

3 comments

0

Maybe make it so it runs the same thing when the player has no scikoins?

0

hm ok...

0

@exarlus i don't get it tho

2 answers

✓ Accepted answer

The logic to your script is wrong. Look at your last if statement, it's inside the else. This means that if statement can only run if plr does not have enough money AND already has the item.

	local plr = game.Players.LocalPlayer
	local Cloud = game.ReplicatedStorage["Flying Cloud"]
	local button = script.Parent
	  
	button.MouseButton1Click:Connect(function()
	if plr.leaderstats.Scikoins.Value == 100 or plr.leaderstats.Scikoins.Value >= 100 then
		if plr.Backpack:FindFirstChild("Flying Cloud") then
			warn(plr.Name.."already has a cloud")
		else
			plr.leaderstats.Scikoins.Value = plr.leaderstats.Scikoins.Value - 100      
			Cloud:Clone().Parent = plr.Backpack
			wait(2)
			warn(plr.Name.."has bought a cloud")
		end		
	else
		warn(plr.Name.."has not enough scikoins")       
	end
	end)

Remember that your code checks from top to bottom, and left to right. I thinks that's the main problem (I didn't check it though).

local plr = game.Players.LocalPlayer
local Cloud = game.ReplicatedStorage["Flying Cloud"]
local button = script.Parent


button.MouseButton1Click:Connect(function()

   if plr.Backpack:FindFirstChild("Flying Cloud") then
            warn(plr.Name.."already has a cloud")
		return -- returns the function
        end

    if plr.leaderstats.Scikoins.Value == 100 or plr.leaderstats.Scikoins.Value >= 100 then
plr.leaderstats.Scikoins.Value = plr.leaderstats.Scikoins.Value - 100       
Cloud:Clone().Parent = plr.Backpack
        wait(2)
        warn(plr.Name.."has bought a cloud")
        else
        warn(plr.Name.."has not enough scikoins")
       -- if plr.Backpack:FindFirstChild("Flying Cloud") then -- switch this so it is above the buy system and the script reads it first before subtracting money from the player
          --  warn(plr.Name.."already has a cloud")
       -- end
    end
end)

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