Starflyer
Starflyer

Reputation: 53

Roblox How to fix If statement?

I'm currently having some problems with a weapon I'm working on for my game. The weapon isn't a tool, but instead is connected to a StarterCharacter. I've made the following script to check if the weapon is equipped, then allow the player to attack.

Mouse.Button1Down:connect(function()
    if isEquipped == true then
        if not pause then
            pause = true
            anim1:Play()
            wait(0.1)
            trail.Enabled = true
            wait(0.6)
            trail.Enabled = false
            pause = false
        end
    end 
end)

handle.Touched:connect(function(hit)
    if isEquipped == true then
        if not pause2 then
            pause2 = true
            if Mouse.Button1Down then
                if humanoid and humanoid.Health > 0 and hit and not hit:isDescendantOf(person) then
                    local target = hit.Parent:FindFirstChild("Humanoid")
                    if target and target.Health > 0 then
                        target:TakeDamage(damage)
                        wait(0.7)
                        pause2 = false
                    end
                end
            end
        end
    end
end)

game.Players.LocalPlayer:GetMouse().KeyDown:Connect(function(KeyPressed)
        if KeyPressed == "e" then
            if not pause3 then
                pause3 = true
                if toggle == false then
                    toggle = true
                    isEquipped.Value = true
                    fake1.Transparency = 1
                    fake2.Transparency = 1
                    disc1.Transparency = 0
                    disc2.Transparency = 0
                    wait(0.7)
                    pause3 = false
                else
                    pause3 = true
                    toggle = false
                    isEquipped.Value = false
                    fake1.Transparency = 0
                    fake2.Transparency = 0
                    disc1.Transparency = 1
                    disc2.Transparency = 1
                    wait(0.7)
                    pause3 = false
            end
        end
    end
end)

The problem is that I can equip the weapon, but when it's equipped, I can't attack with it. Would appreciate some help. Thanks in advance!

Upvotes: 2

Views: 408

Answers (1)

user10125117
user10125117

Reputation:

In the code you shown, there is no section or part of the code that actually sets isEquipped to true. You should bind this value to tool.Equipped event.

If you want more information about this, I suggest you to visit Roblox Wiki.

Also, are you using a LocalScript, or a Script? If you're using a LocalScript, you should use RemoteEvents for things that affect other clients, for example, sounds, settings a part's properties, creating parts, etc, etc. You can find more detailed infomration about this Client-Server communication model in Roblox Wiki.

Also, what is pause3's value when the program start? What value do you asign to this variable? It should be set to false since the program starts, otherwise you will not be able to attack.

Upvotes: 1

Related Questions