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

How can I fix my else section in my function? [Answered]

My script does not seem to go to my else section at all. Here's the script

sp = script.Parent
Debounce = false


sp.Touched:connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	local Human = hit.Parent:FindFirstChild("Humanoid")
	if Human ~= nil then
		print("Human is true")
		if player.Backpack:FindFirstChild("Sword") == nil then
			if Debounce == false then Debounce = true
				print("Player does not have tool!")
				local Tool = game.ReplicatedStorage:FindFirstChild("Sword")
				Tool = Tool:Clone()
				Tool.Parent = player.Backpack
				Debounce = false
			else --Problem here
			if Debounce == false then Debounce  = true
				print("Player does have tool!")
				local msg = Instance.new("Message", player.PlayerGui)
				msg.Text = "You cannot get this tool twice!"
				wait(2)
				Debounce = false
				msg:remove()
				end
			end
		end
	end
end)

At line 17, I do not even see in the output it saying "Player does have tool!" Did I mess up and end or something? I have tried making line 17 become

elseif player.Backpack:FindFirstChild("Sword") ~= nil then

1 answer

✓ Accepted answer

This seems to be a matter of logic. The first thing I noticed was that if line 11 does not pass, line 18 will not pass either. This is because both are checking for Debounce == false. This leads me to think that you may want to put an end before line 17, and remove one of the ends that you have at the end of your code.

Again, more information would be very helpful.

Something like this...

sp = script.Parent
Debounce = false


sp.Touched:connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    local Human = hit.Parent:FindFirstChild("Humanoid")
    if Human ~= nil then
        print("Human is true")
        if player.Backpack:FindFirstChild("Sword") == nil then
            if Debounce == false then Debounce = true
                print("Player does not have tool!")
                local Tool = game.ReplicatedStorage:FindFirstChild("Sword")
                Tool = Tool:Clone()
                Tool.Parent = player.Backpack
                Debounce = false
            end
         else
            if Debounce == false then Debounce  = true
                print("Player does have tool!")
                local msg = Instance.new("Message", player.PlayerGui)
                msg.Text = "You cannot get this tool twice!"
                wait(2)
                Debounce = false
                msg:remove()
            end
        end
    end
end)

P.S. Consider tidying up your code and logic! It will help you in the long run for debugging cases like these.

2 comments

0

Thank you, I fixed my Debug as well. (This works)

0

Cleaned up a little more: http://hastebin.com/rocuxexola.lua

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