If statement not working

LocalScript:

--Services
local repStorage = game:GetService("ReplicatedStorage")
local plr = game:GetService("Players")
--Modules
local varModule =  require(repStorage.varModule)
--Local Player
local localPlr = plr.LocalPlayer
--GUI
local playerGUI = localPlr:WaitForChild("PlayerGui")
local weaponName = playerGUI:WaitForChild("WeaponName")
local name = weaponName:WaitForChild("Name")

local function changeName(itemName)
	name.Text = itemName
end

while true do
	print(varModule.GUN_EQUIPPED)
	wait()
end

if varModule.GUN_EQUIPPED then
	local itemName = "Silenced Pistol"
	changeName(itemName)
end

ModuleScript:

local variables = {
	
	LOCKPICKING = false,
	LOCKPICK_COUNT = 0,
	LOCKPICK_COUNTMAX = 2,
	GUN_EQUIPPED = false,
	LOCKPICK_EQUIPPED = false,
	AMMO = 12, -- stores the max amount the ammo can be
	LEFT_LEAN_KEYBIND = Enum.KeyCode.Q,
	RIGHT_LEAN_KEYBIND = Enum.KeyCode.E,
	AIMING_KEYBIND = Enum.UserInputType.MouseButton2,
	
	
}

return variables

Problem?: In the local script, the if statement should run when GUN_EQUIPPED is true. It is true but the if statement does not run for some reason.

What’s setting the variable’s value? Can you show the lines that are modifying it and what type of script it is?

LocalScript:

local varModule = require(game:GetService("ReplicatedStorage").varModule)
local repStorage = game:GetService("ReplicatedStorage")
local plr = game.Players.LocalPlayer
local suppGun = script.Parent.Parent
local uIS = game:GetService("UserInputService")
local cam = game.Workspace.Camera
local char = plr.Character

local runSer = game:GetService("RunService") -- gets the run service service



local Pistol

suppGun.Equipped:Connect(function() -- checks if gun is equipped
	varModule.GUN_EQUIPPED = true
	plr.CameraMode = Enum.CameraMode.LockFirstPerson
	repStorage.Pistol:Clone().Parent = cam -- creates a clone of the viewmodel and makes the parent of the clone the camera
end)

suppGun.Unequipped:Connect(function() --checks if gun is unequipped
	varModule.GUN_EQUIPPED = false 
	plr.CameraMode = Enum.CameraMode.Classic
	Pistol = cam.Pistol -- when the gun is unequipped, the viewmodel will be deleted / destroyed
	Pistol:Destroy()
end)

runSer.RenderStepped:Connect(function() -- whilst the player is playing
	if char.Humanoid.Health <= 0 then -- checks if the player is dead or not
		if cam:FindFirstChild("Pistol") ~= nil then -- if they are dead, the viewmodel is destroyed
			cam.Pistol:Destroy()
		end
	end
	
	if varModule.GUN_EQUIPPED == true then -- if the player is alive and the gun is equipped then 
		if cam:FindFirstChild("Pistol") ~= nil then -- checks if the camera has the viewmodel as their child
			cam.Pistol:SetPrimaryPartCFrame(cam.CFrame) -- sets the viewmodels CFrame to the camera's CFrame
			for i, v in pairs(cam.Pistol:GetChildren()) do -- for every part inside of the viewmodel
				if v:IsA("BasePart") then -- checks if the part is a base part
					v.CanCollide = false -- turns off the parts collision hitbox
				end
			end
			
			cam.Pistol:SetPrimaryPartCFrame(cam.CFrame) -- sets the viewmodels CFrame to the camera's CFrame
		end
	end
end)

while true do
	print(varModule.GUN_EQUIPPED)
	wait()
end

This snippet of code will always yield the code after it because it never breaks. If it never breaks, no code after it will run indefinitely. Alternatively, you can wrap it inside a new thread to overcome this issue.

task.spawn(function()
    while true do
	    print(varModule.GUN_EQUIPPED)
	    wait()
    end
end)

your alternative works but the problem is still there

You also have to constantly check instead of doing a one time check. There’s many ways to do it but using a while loop will fulfil this need.

while true do
    task.wait()
    if varModule.GUN_EQUIPPED then
	    local itemName = "Silenced Pistol"
	    changeName(itemName)
    end
end
1 Like

The code here works, thanks for your help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.