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

Local script if statement doesn't work?

Asked by 1 year ago

So, I have an if statement that checks if the player has a key, and if the player doesn't have it, it plays a sound. But I have the key and the door doesn't open. The script is also a local script.

game.Players.LocalPlayer.CharacterAdded:Wait()
local Door = workspace.Door.MyBigPPpart
local d = false
local TweenService = game:GetService("TweenService")

Door.ProximityPrompt.Triggered:Connect(function(player)
    if not d then
        d = true
        local anim = player.Character.Humanoid:LoadAnimation(script.Pickup)
        anim:Play()
        if not player.Backpack:FindFirstChild("Door1Key") or not player.Character:FindFirstChild("Door1Key") then
            Door.Lock:Play()
        elseif player.Backpack:FindFirstChild("Door1Key") or player.Character:FindFirstChild("Door1Key") then
            local info = TweenInfo.new(
                1,
                Enum.EasingStyle.Linear,
                Enum.EasingDirection.In,
                0,
                false,
                0
            )

            local goals = {
                Position = Vector3.new(470.108, -19.667, 1597.603);
                Orientation = Vector3.new(0, 90, 0)
            }

            local ts = TweenService:Create(Door.Parent.Door1, info, goals)
            ts:Play()

            local info1 = TweenInfo.new(
                1,
                Enum.EasingStyle.Linear,
                Enum.EasingDirection.In,
                0,
                false,
                0
            )

            local goals1 = {
                Position = Vector3.new(481.541, -19.667, 1597.603);
                Orientation = Vector3.new(0, -90, 0)
            }

            local ts1 = TweenService:Create(Door.Parent.Door2, info1, goals1)
            ts1:Play()
            Door.Open:Play()
        end
        wait(1)
        d = false
    end
end)

1 answer

Log in to vote
0
Answered by 1 year ago
Edited 1 year ago

If you want to check if the player doesn't have the key, you should be checking his Backpack and his Character, and not just his Backpack or his Character. If you use or, it will also be true since he can only have it at one place at once, even if he does have the key.

Basically: Replace or with and in the first if-statement (the one that checks if the player does not have the key).

Ad

Answer this question