So I am trying to make a scope for my gun when you right click your mouse. But when you unequip the gun, you can still use the scope, which I don’t want. Here is my script:
cursor.Button2Down:Connect(function()
game.Workspace.CurrentCamera.FieldOfView = 40
client.Character:WaitForChild("Humanoid").WalkSpeed = 14.5
plrgui:WaitForChild("HotBarGUI").Enabled = false
end)
cursor.Button2Up:Connect(function()
game.Workspace.CurrentCamera.FieldOfView = 70
client.Character:WaitForChild("Humanoid").WalkSpeed = 17.5
plrgui:WaitForChild("HotBarGUI").Enabled = true
end)
Please help soon,
papahetfan
1 Like
You first want to check if the weapon is currently equipped or not. To do this, insert a BoolValue inside the tool (I assume your script is in), and rename the BoolValue to “EquippedValue”.
Now you want to add 2 functions in the script to detect if the weapon is equipped or not, and change the BoolValue accordingly.
Then you just want to mention if the BoolValue is true or false when checking if the player pressed their mouse or not.
Here is your script:
local tool = script.Parent -- I assume your script is a direct child of the tool
local BoolValue = script.Parent:WaitForChild("EquippedValue")
-- Change value
tool.Equipped:Connect(function()
BoolValue.Value = true
end)
tool.Unequipped:Connect(function()
BoolValue.Value = false
-- if, incase, the player unequips the gun while in scope
game.Workspace.CurrentCamera.FieldOfView = 70
client.Character:WaitForChild("Humanoid").WalkSpeed = 17.5
plrgui:WaitForChild("HotBarGUI").Enabled = true
end)
-- Main part
cursor.Button2Down:Connect(function()
if BoolValue.Value == true then -- checks if gun is equipped
game.Workspace.CurrentCamera.FieldOfView = 40
client.Character:WaitForChild("Humanoid").WalkSpeed = 14.5
plrgui:WaitForChild("HotBarGUI").Enabled = false
end
end)
cursor.Button2Up:Connect(function()
if BoolValue.Value == true then -- Checks if gun is equipped
game.Workspace.CurrentCamera.FieldOfView = 70
client.Character:WaitForChild("Humanoid").WalkSpeed = 17.5
plrgui:WaitForChild("HotBarGUI").Enabled = true
end
end)
3 Likes
Thank you for letting me know, I have edited the code.
I believe it’s better to use and rely on a Value Instance rather than relying on a variable in the script. It doesn’t really matter but yea.
1 Like
Thank you so much, your the best!
2 Likes
I know I may not had you as the solution but you also helped my out too so that the player can’t bug out the weapon, thank you too!