I’m currently working on a weapon script. The player clicks on a target, waits a few seconds, then fires a volley of bullets. Part of the script is that when the player clicks on a target, they face it. Now everything works until the player finishes the firing sequence, at which point they continue to face the target, instead of being able to move freely again
The script uses an “aiming” variable as a debounce and for some other procedures, and at the beginning it checks whether the player is aiming or not. When trying to check if the player is aiming for the RenderStep function, it underlines the “if then” function with the following error:
The script:
mouse.Button1Down:Connect(function()
if tool.Parent == game.Workspace:WaitForChild(player.Name) then -- Check if Tool is Equipped
if mouse.Target ~= nil then
local target = mouse.Target.Parent -- Locate Target
local playerTargetRoot = target:FindFirstChild("HumanoidRootPart") -- Locate Target Root Part
if target:FindFirstChildOfClass("Humanoid") then -- Detect Humanoid
if aiming == false then -- Check if Player is Aiming
local distance = (playerTargetRoot.Position - playerRoot.Position).Magnitude -- Get Distance Between Player Root and Target Root
if distance <= range.Value then -- Check if in Range
local targetHum = target:FindFirstChildOfClass("Humanoid") -- Locate Target Humanoid
aiming = true -- Trigger the Aiming
-- Facing Target
if aiming == true then
coroutine.wrap(function()
runService.RenderStepped:Connect(function(step)
playerRoot.CFrame = CFrame.new(playerRoot.Position,Vector3.new(playerTargetRoot.Position.X, playerRoot.Position.Y, playerTargetRoot.position.Z))
end
end)
end)()
If anyone knows a solution to this, or another way of facing the target only while aiming, please let me know
Why even bother with the check? aiming is always going to be true at that point in the code because your setting it immediately before:
if aiming == false then
--
aiming = true
--
if aiming == true then
--
end
end
Unless you didn’t want to check if aiming is true inside the if aiming==false check - in which case you need to add an end to the check before testing if aiming is true:
if aiming == false then
--
aiming = true
--
end
if aiming == true then
--
end
I checked if aiming was true so that the player can’t lock on multiple times and fire multiple volleys at once. I need to check if aiming for the RenderStep is true so that the player only locks on while they are aiming the weapon.
I’m not saying you shouldn’t check if aiming is true at other points in your code - there’s just no use checking it at the point in the code above because your setting it to true immediately before checking if it’s true - so there is no point in checking it.
Yes, but the RenderStep function has to be aware of when the player is aiming and when they aren’t, as to make sure that they only face the target while aiming. I may be misunderstanding you, but unless there is another way of informing the function that the player is aiming, then checking the aim is required.
It could just be the indentation not forming correctly when copying onto the forum, or that we don’t have the full code block, but, it looks like you’ve added an extra end into the coroutine.
One for the “if then”, one for the RenderStep function, and one for the coroutine.wrap function. They don’t have any red underlines so I’m guessing it’s the correct amount.
I think you need to remove the aiming=true check outside of the aiming=false check:
if aiming == false then -- Check if Player is Aiming
local distance = (playerTargetRoot.Position - playerRoot.Position).Magnitude -- Get Distance Between Player Root and Target Root
if distance <= range.Value then -- Check if in Range
local targetHum = target:FindFirstChildOfClass("Humanoid") -- Locate Target Humanoid
aiming = true -- Trigger the Aiming
-- Facing Target
end
end
if aiming == true then
coroutine.wrap(function()
runService.RenderStepped:Connect(function(step)
playerRoot.CFrame = CFrame.new(playerRoot.Position,Vector3.new(playerTargetRoot.Position.X, playerRoot.Position.Y, playerTargetRoot.position.Z)) end)
end)()
end
This causes issues later on in the script, as some of the variables created in this segment are used outside of the ending point. Moving the ends just results in the same problem I had initially.
Why are you checking a boolean value with == true?
In lua, you can simply do if aiming then
There is no need to compare a boolean with another boolean if the if statement already checks for a boolean.
If you want false, just do if not aiming then.
This is a great piece of information to keep in mind. Only issue is that while the function detects when the player is aiming, it doesn’t seem to make the player face the target. Not sure why, but I’m sure with some troubleshooting I can fix it. Thanks for the help!