Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

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

Asked by 9 years ago

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

Log in to vote
1
Answered by
Unclear 1776 Moderation Voter
9 years ago

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.

0
Thank you, I fixed my Debug as well. (This works) alphawolvess 1784 — 9y
0
Cleaned up a little more: http://hastebin.com/rocuxexola.lua BlueTaslem 18071 — 9y
Ad

Answer this question