Not cloning for people above ranks

This title is probably really bad. And I will try to explain it a little better. So I have made a self check-in that lets you choose if you want there to be 3 classes or 4 classes. If you type true for one variable, it makes it only 3 classes. If you type false for the one variable it makes it 4 classes. (The variable is called Class3 and you will see it in my script.) It then transfers into a bool value and either makes it true or false. My issue is, I am trying to make it so if you are a higher rank above and passenger rank, like a ground crew member or something like that, it gives you the best kind of ticket. But every time it will not clone any ticket. I was wondering if one of you guys could look over and try to find why it isn’t cloning properly and maybe try to find some bugs within my script. Thanks.

Script


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local debounce = false
local Values = script.Parent.Values
local BusinessClassName = Values.BusinessClassName.Value
local BusinessClassTicket = Values.BusinessClassTicket
local BusinessRank = Values.BusinessRank.Value
local Class3 = Values.Class3.Value
local EconomyClassName = Values.EconomyClassName.Value
local EconomyClassTicket = Values.EconomyClassTicket
local EconomyRank = Values.EconomyRank.Value
local FirstClassName = Values.FirstClassName.Value
local FirstClassTicket = Values.FirstClassTicket
local FirstClassRank = Values.FirstClassRank.Value
local InvestorClassName = Values.InvestorClassName.Value
local InvestorClassTicket = Values.InvestorClassTicket
local InvestorRank = Values.InvestorRank.Value
local groupID = Values.groupID.Value
local Gui = script.Parent:WaitForChild("LoadingGUI")
local GuiClone = Gui:Clone()
GuiClone.Parent = game.ReplicatedStorage



local Brick = script.Parent.SCIParts.Touch

Brick.Touched:Connect(function(hit)
	if debounce then return end
    if hit.Parent:FindFirstChild("Humanoid") then
        local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
	    if Player then	
            local Gui = game.ReplicatedStorage:WaitForChild("LoadingGUI")
            local GuiClone = Gui:Clone()
		    GuiClone.Parent = Player:WaitForChild("PlayerGui")
			    debounce = true
			wait(5)
			elseif Player:GetRankInGroup(groupID) == EconomyRank then
				local ECT = ReplicatedStorage:WaitForChild(EconomyClassTicket.Value):Clone()
				ECT.Parent = Player:WaitForChild("Backpack")
			elseif Player:GetRankInGroup(groupID) == BusinessRank  then
					local BCT = ReplicatedStorage:WaitForChild(BusinessClassTicket.Value):Clone()
					BCT.Parent = Player:WaitForChild("Backpack")
			elseif Class3 == false and Player:GetRankInGroup(groupID) == FirstClassRank then
					local FCT = ReplicatedStorage:WaitForChild(FirstClassTicket.Value):Clone()
						FCT.Parent = Player:WaitForChild("Backpack")				
			elseif Class3 == true and Player:GetRankInGroup(groupID) >= FirstClassRank then
					local FCT = ReplicatedStorage:WaitForChild(FirstClassTicket.Value):Clone()
						FCT.Parent = Player:WaitForChild("Backpack")
			elseif Class3 == true then return  
			elseif  Class3 == false and Player:GetRankInGroup(groupID) >= InvestorRank then
				local ICT = ReplicatedStorage:WaitForChild(InvestorClassTicket.Value):Clone()		
						ICT.Parent = Player:WaitForChild("Backpack")
			    
			
			
			

		
		
		end
		
	end
end)

1 Like

I’m confused here, is there any errors?

1 Like

Your indentation is all over the place.

Your condition of if Player then is not encompassing the rest of the conditions, so the only time the other blocks will run is when there’s no player, in which case they will error.

You need to format your code properly and understand what you’re trying to do. Ensure every condition is correctly closed where you want it to.

1 Like

So how do I format it so the other blocks will run?

1 Like

No. 30 charrsssssssssssssssssssss

1 Like

Why are you over complicating things?
Why don’t you use the variable for the number instead of making the variable for a value inside of the script?

1 Like

I am using a READ ME script. I am making a self- check-in for someone else until I ran into this problem. And I don’t want to give them the main script.

Just nest it correctly. elseif statements only run if the if is false. If Player doesn’t exist then the elseif will error because it indexes Player which you’ve just checked does not exist.

It needs to look like this:

if Player then
    if Player:GetRankInGroup(groupID) == EconomyRank then
        local ECT = ReplicatedStorage:WaitForChild(EconomyClassTicket.Value):Clone()
        ECT.Parent = Player:WaitForChild("Backpack")
    elseif Player:GetRankInGroup(groupID) == BusinessRank  then
        -- ........
    end
end

Player does exist. It is on line 31.

 if Player then	
            local Gui = game.ReplicatedStorage:WaitForChild("LoadingGUI")
            local GuiClone = Gui:Clone()
		    GuiClone.Parent = Player:WaitForChild("PlayerGui")
			    debounce = true
			wait(5)
1 Like

Change the first elseif to if.

If that block runs, Player exists, in which case the elseif blocks will not run.

And in the event Player does not exist, the entire thing errors.

1 Like

So you are saying I have to get rid of that line?

That’s not what I’m saying. I gave a bit of example code in my earlier reply to explain the layout required.

1 Like

Ah ok. 30 charresss.ssssssssssssssssssss

So how does this look?

Brick.Touched:Connect(function(hit)
	if debounce then return end
    if hit.Parent:FindFirstChild("Humanoid") then
        local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
	    if Player then	
            local Gui = game.ReplicatedStorage:WaitForChild("LoadingGUI")
            local GuiClone = Gui:Clone()
		    GuiClone.Parent = Player:WaitForChild("PlayerGui")
		    debounce = true
			wait(5)
		if Player then	
			elseif Player:GetRankInGroup(groupID) == EconomyRank then
				local ECT = ReplicatedStorage:WaitForChild(EconomyClassTicket.Value):Clone()
				ECT.Parent = Player:WaitForChild("Backpack")
			elseif Player:GetRankInGroup(groupID) == BusinessRank  then
				local BCT = ReplicatedStorage:WaitForChild(BusinessClassTicket.Value):Clone()
				BCT.Parent = Player:WaitForChild("Backpack")
			elseif Class3 == false and Player:GetRankInGroup(groupID) == FirstClassRank then
				local FCT = ReplicatedStorage:WaitForChild(FirstClassTicket.Value):Clone()
				FCT.Parent = Player:WaitForChild("Backpack")				
			elseif Class3 == true and Player:GetRankInGroup(groupID) >= FirstClassRank then
				local FCT = ReplicatedStorage:WaitForChild(FirstClassTicket.Value):Clone()
				FCT.Parent = Player:WaitForChild("Backpack")
			elseif Class3 == true then return  
			elseif  Class3 == false and Player:GetRankInGroup(groupID) >= InvestorRank then
				local ICT = ReplicatedStorage:WaitForChild(InvestorClassTicket.Value):Clone()		
						ICT.Parent = Player:WaitForChild("Backpack")
			    
			
			
			

		
		    end 
		end
		
	end
end)

We’re not really meant to just give whole bits of code here, but you’re in the process of learning Lua and I think in this case it will help you to understand the different blocks a bit better if I simply fix the indentation and change the blocks and keywords to what I think you’re trying to do.

Here is the edited code:

-- All of your global definitions here that I won't bother repeating!


Brick.Touched:Connect(function(hit)
	if debounce then
		-- In the event that this event is triggered whilst we are still
		-- dealing with a previous event, we want to exit early and not
		-- bother with the rest of the logic. return does this for us.
		return
	end

	if hit.Parent:FindFirstChild("Humanoid") then
		-- A Humanoid was found, now let's find the Player it belongs to
		local Player = game.Players:GetPlayerFromCharacter(hit.Parent)

		if Player then
			-- The player was found...

			-- Firstly, I'll set debounce to true so we don't end up doing
			-- the same action multiple times.
			debounce = true

			-- Secondly, let's clone the GUI into their PlayerGui
			local Gui = game.ReplicatedStorage:WaitForChild("LoadingGUI")
			local GuiClone = Gui:Clone()
			GuiClone.Parent = Player:WaitForChild("PlayerGui")
			
			-- Thirdly, we can check their rank and provide the appropriate
			-- ticket tool into their backpack. There's no need to keep calling
			-- the same API again and again, so I'll store the result once.
			local rank = Player:GetRankInGroup(groupID)

			-- To save us having to have this bulky statement on every line,
			-- we can just wait for Backpack once at the start.
			Player:WaitForChild("Backpack")

			-- And again to help us out later, as you'll see, lets have a
			-- variable that we set to the ticket we would like to clone.
			local ticket = nil

			-- Now onto the rank conditions...
			if rank == EconomyRank then
				-- Provide Economy ticket.
				ticket = ReplicatedStorage:WaitForChild(EconomyClassTicket.Value)

			elseif rank == BusinessRank  then
				-- Provide Business ticket.
				ticket = ReplicatedStorage:WaitForChild(BusinessClassTicket.Value)

			elseif Class3 == false then
				-- Class3 is false, therefore Investors get a special ticket and
				-- First Class get the first class ticket.

				if rank == FirstClassRank then
					ticket = ReplicatedStorage:WaitForChild(FirstClassTicket.Value)

				elseif rank >= InvestorRank then
					ticket = ReplicatedStorage:WaitForChild(InvestorClassTicket.Value)	
	
				end -- This ends our subset of conditions for when Class3 is false.

			elseif rank >= FirstClassRank then
				-- We don't need to check if Class3 == true, as it's the last
				-- condition and only runs if all the others didn't - in this
				-- case, the previous block would run if Class3 == false, so for
				-- this block to run, we can assume Class3 == true.
				ticket = ReplicatedStorage:WaitForChild(FirstClassTicket.Value)

			end -- This ends our big chunk of rank conditions.

			-- In theory we have selected a ticket, but in some cases if the user
			-- doesn't satisfy any rank condition, we have no ticket. Let's check
			-- that we have a ticket first.
			if ticket then

				-- Now here's another check we should do - does the player
				-- already have a ticket matching this one? If so it doesn't
				-- make much sense to give them another one.
				local alreadyHasTicket = Player.Backpack:FindFirstChild( ticket.Name ) or Player.Character:FindFirstChild( ticket.Name )

				if not alreadyHasTicket then
					-- The player doesn't have this ticket yet, so let's
					-- give it to them.
					ticket:Clone().Parent = Player.Backpack

				end -- This ends our "already has ticket" check.

			end -- This ends our "did we select a ticket" check.

			-- Fourthly, we now wait *after* we've done all our logic, to give
			-- the player chance to move away from the brick so they don't
			-- trigger it again.
			wait(5)

			-- Finally, and very importantly, we need to release that debounce
			-- we set earlier. This needs to happen inside the same block level
			-- where you set it to ensure it will reset in the same conditions
			-- as it was set. If it were in another condition, you might find
			-- yourself in a situation where you set debounce = true, and it
			-- *never* gets set back to false. That would disable your event forever.
			debounce = false

		end -- This ends our Player exists check.

	end -- This ends out Humanoid exists check.

end) -- This ends the connected function.

Important notes:

  • The if and elseif conditions throughout. Note the indentation to keep track of what is open and closed. Read my comments explaining each condition.
  • You never set debounce to false - this is absolutely critical.
  • You did return when Class3 was false in a particular case. This would have prevented your debounce from clearing to false even if you did have the statement in there.
  • There was no check to see if the player already had a ticket - in theory you could end up filling your backpack with these tickets. I’ve added a check to see if this is the case.

I’ve exaggerated some of the line spacing to make it a bit easier to read the comments about each bit of code.

Please read the comments and understand what’s going on here. I had to make a few assumptions here and there to understand what you were trying to do and what type of object things were. Let me know if you run into any further issues and any errors in the output window.

Does not work. There are no errors. Thanks though.

1 Like

I don’t mean to be awkward or annoying, but I highly doubt that it would both not work and have no errors or warnings at all.

The only.explanation I can think of is the location of this script, whether it runs at all, the type of script this is and therefore whether it’s on the server or the client, and perhaps some issue with the tickets themselves.

Can you let us know what type of script it is, where it is located, what type of object all the tickets are, and whether any of the logic works at all (e.g. the GUI) in which case it will just be an issue with all your variables and values that feed into the conditions.

Also are you testing in Studio or a live server?

It is a regular server script, it is located inside of a model, and the tickets are tools. The GUI is working properly. It is being cloned right, it is just the cloning of the tool. I am testing in a live server.

If the GUI works right and you confirm there are no errors or warnings on the Server tab of the Developer Console (opened using F9, note it defaults to client log) then it’s one of:

  • Incorrect rank values that don’t correspond with the group rank of the player who touched the brick.
  • The player already has a ticket by the same name in their Backpack or their Character.

Anything else, I believe would make an error or a warning.