Hello, I have a problem encountering and don’t have any solutions anymore to fix this.
(What I wanted to do)
I wanted to try creating a gun in a way which I create the main functions from a module, and then calling it from the Local Script.
(The issue I am encountering)
function gunModule.Scoping(enable)
if enable == true then
if gunModule.state.Disable then return end
gunModule.scopeState.scopeIdleAnim:Play()
gunModule.scopeState.IsScoping = true
gunModule.plrObject.hum.CameraOffset = Vector3.new(3,0,0)
gunModule.plrObject.hum.UseJumpPower = true
gunModule.plrObject.hum.JumpPower = 0
gunModule.plrObject.hum.AutoRotate = false
gunModule.plrObject.Plr.CameraMaxZoomDistance = 5.5
gunModule.plrObject.Plr.CameraMinZoomDistance = 5.5
uis.MouseIconEnabled = true
gunModule.state.playerMouse.Icon = "http://www.roblox.com/asset/?id=15722065980"
local property = {}
property.FieldOfView = 50
local scopeTween = TS:Create(gunModule.plrObject.camera, TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.In), property)
scopeTween:Play()
runService:BindToRenderStep("Scoping", Enum.RenderPriority.Camera.Value + 1, function()
uis.MouseBehavior = Enum.MouseBehavior.LockCenter
local _, y = gunModule.plrObject.camera.CFrame.Rotation:ToEulerAnglesYXZ()
TS:Create(gunModule.plrObject.hrp, TweenInfo.new(0.05, Enum.EasingStyle.Linear, Enum.EasingDirection.In), {CFrame = CFrame.new(gunModule.plrObject.hrp.Position) * CFrame.Angles(0,y,0)}):Play()
end)
else
gunModule.scopeState.scopeIdleAnim:Stop()
gunModule.scopeState.IsScoping = false
gunModule.plrObject.hum.JumpPower = 50
gunModule.plrObject.hum.UseJumpPower = false
gunModule.plrObject.hum.CameraOffset = Vector3.new(0, 0, 0)
runService:UnbindFromRenderStep("Scoping")
uis.MouseIconEnabled = false
--gunModule.state.playerMouse.Icon = ""
gunModule.plrObject.hum.AutoRotate = true
gunModule.plrObject.Plr.CameraMaxZoomDistance = 128
gunModule.plrObject.Plr.CameraMinZoomDistance = 0.5
local resetTween = TS:Create(gunModule.plrObject.camera, TweenInfo.new(0.3, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {FieldOfView = 70})
resetTween:Play()
end
end
This function although when the disable isn’t called nor it is made to stop at a certain limit of time, it seems that the function doesn’t work as it intended.
I want it to keep the camera to its side, even while moving, this is what I wanted that function to do.
When the function is called, it works fine that is if that player does not perform any input (keyboards, mouse moving etc) PS: excluding its activation call
When I print the MouseBehavior, it stays still at “Enum.MouseBehavior.LockCenter”, and If i perform a movement or an input, the mouse behavior changes to “Enum.MouseBehavior.LockCurrentPosition”
I also tried forcefully placing it into another loop to check if mouse behavior is not lock center and then forcing it to stay at lock center, but it is still overriden by the input somehow.
Here is a video of the issue:
As I stay still while activating the scope, it works fine, however If I made an input such as pressing keys, the function stops working as I have intended for it to do.
The scoping isn’t even disabled or even called to be disabled unless a player releases its activation key. What is the problem? Is there a way to fix this?
Here is my entire module script
local gunModule = {}
local uis = game:GetService("UserInputService")
local runService = game:GetService("RunService")
local TS = game:GetService("TweenService")
gunModule.state = {}
gunModule.plrObject = {}
gunModule.rollingConfig = {}
gunModule.MobileUI = {}
gunModule.gunConfig = {}
gunModule.gunEvents = {}
gunModule.scopeState = {}
local function loadData()
gunModule.state.playerMouse = nil
gunModule.state.ExpectingInput = false
gunModule.state.isMouse1down = false
gunModule.state.isMouse2down = false
gunModule.state.Disable = false
gunModule.plrObject.Plr = nil
gunModule.plrObject.PlrGui = nil
gunModule.plrObject.char = nil
gunModule.plrObject.hum = nil
gunModule.plrObject.hrp = nil
gunModule.plrObject.camera = nil
gunModule.rollingConfig.isRolling = false
gunModule.rollingConfig.canRoll = false
gunModule.rollingConfig.rollSpeed = false
gunModule.rollingConfig.rollDuration = false
gunModule.rollingConfig.rollAnim = nil
gunModule.rollingConfig.rollCooldown = 2
gunModule.MobileUI.mobileAssistant = nil
gunModule.MobileUI.crossHairMobile = nil
gunModule.MobileUI.reloadMobile = nil
gunModule.MobileUI.FireMobile = nil
gunModule.MobileUI.rollMobile = nil
gunModule.MobileUI.sprintMobile = nil
gunModule.gunConfig.GunUI = nil
gunModule.gunConfig.isReloading = nil
gunModule.gunConfig.maxBullet = nil
gunModule.gunConfig.currentBullet = nil
gunModule.gunConfig.isFiring = false
gunModule.gunConfig.gunCooldown = nil
gunModule.gunEvents.sprintEvent = nil
gunModule.gunEvents.firingEvent = nil
gunModule.scopeState.scopeIdleAnim = nil
gunModule.scopeState.IsScoping = false
end
loadData()
local rollMovementControl
function gunModule.Scoping(enable)
if enable == true then
if gunModule.state.Disable then return end
gunModule.scopeState.scopeIdleAnim:Play()
gunModule.scopeState.IsScoping = true
gunModule.plrObject.hum.CameraOffset = Vector3.new(3,0,0)
gunModule.plrObject.hum.UseJumpPower = true
gunModule.plrObject.hum.JumpPower = 0
gunModule.plrObject.hum.AutoRotate = false
gunModule.plrObject.Plr.CameraMaxZoomDistance = 5.5
gunModule.plrObject.Plr.CameraMinZoomDistance = 5.5
uis.MouseIconEnabled = true
gunModule.state.playerMouse.Icon = "http://www.roblox.com/asset/?id=15722065980"
local property = {}
property.FieldOfView = 50
local scopeTween = TS:Create(gunModule.plrObject.camera, TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.In), property)
scopeTween:Play()
runService:BindToRenderStep("Scoping", Enum.RenderPriority.Camera.Value + 1, function()
uis.MouseBehavior = Enum.MouseBehavior.LockCenter
local _, y = gunModule.plrObject.camera.CFrame.Rotation:ToEulerAnglesYXZ()
TS:Create(gunModule.plrObject.hrp, TweenInfo.new(0.05, Enum.EasingStyle.Linear, Enum.EasingDirection.In), {CFrame = CFrame.new(gunModule.plrObject.hrp.Position) * CFrame.Angles(0,y,0)}):Play()
end)
else
gunModule.scopeState.scopeIdleAnim:Stop()
gunModule.scopeState.IsScoping = false
gunModule.plrObject.hum.JumpPower = 50
gunModule.plrObject.hum.UseJumpPower = false
gunModule.plrObject.hum.CameraOffset = Vector3.new(0, 0, 0)
runService:UnbindFromRenderStep("Scoping")
uis.MouseIconEnabled = false
--gunModule.state.playerMouse.Icon = ""
gunModule.plrObject.hum.AutoRotate = true
gunModule.plrObject.Plr.CameraMaxZoomDistance = 128
gunModule.plrObject.Plr.CameraMinZoomDistance = 0.5
local resetTween = TS:Create(gunModule.plrObject.camera, TweenInfo.new(0.3, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {FieldOfView = 70})
resetTween:Play()
end
end
function gunModule.UIHandler()
if not gunModule.plrObject.Plr then return end
if not gunModule.plrObject.PlrGui:FindFirstChild(gunModule.gunConfig.GunUI.Name) then
local clUI = gunModule.gunConfig.GunUI:Clone()
clUI.Parent = gunModule.plrObject.PlrGui
gunModule.MobileUI.crossHairMobile.Visible = true
gunModule.MobileUI.reloadMobile.Visible = true
gunModule.MobileUI.FireMobile.Visible = true
gunModule.MobileUI.rollMobile.Visible = true
mm = gunModule.MobileUI.reloadMobile.InputBegan:Connect(function(input, isTyping)
if isTyping then return end
if input.UserInputType == Enum.UserInputType.Touch then
if gunModule.gunConfig.isReloading.Value == false then
gunModule.gunEvents.firingEvent:FireServer(nil,true)
end
end
end)
local ammoLabel = clUI:WaitForChild("Ammo")
local reloadingLabel = ammoLabel:WaitForChild("Reloading")
ammoLabel.Text = gunModule.gunConfig.currentBullet.Value.."/"..gunModule.gunConfig.maxBullet.Value
gunModule.gunConfig.isReloading.Changed:Connect(function(val)
if val == true then
reloadingLabel.Visible = true
gunModule.Scoping(false)
else
reloadingLabel.Visible = false
end
end)
gunModule.gunConfig.currentBullet.Changed:Connect(function()
ammoLabel.Text = gunModule.gunConfig.currentBullet.Value.."/"..gunModule.gunConfig.maxBullet.Value
end)
end
end
function gunModule.unUI()
if not gunModule.plrObject.Plr then return end
if gunModule.plrObject.PlrGui:FindFirstChild(gunModule.gunConfig.GunUI.Name) then
gunModule.plrObject.PlrGui:FindFirstChild(gunModule.gunConfig.GunUI.Name):Destroy()
end
gunModule.MobileUI.crossHairMobile.Visible = false
gunModule.MobileUI.reloadMobile.Visible = false
gunModule.MobileUI.FireMobile.Visible = false
gunModule.MobileUI.rollMobile.Visible = false
if mm then
mm:Disconnect()
end
end
function gunModule.rollMovement(moveControl)
if not gunModule.plrObject.Plr then return end
if gunModule.rollingConfig.isRolling == false and gunModule.rollingConfig.canRoll == true then
gunModule.rollingConfig.isRolling = true
gunModule.rollingConfig.rollAnim:Play()
local linearVelocity = Instance.new("LinearVelocity")
linearVelocity.MaxForce = math.huge
linearVelocity.VelocityConstraintMode = Enum.VelocityConstraintMode.Vector
local initializingVelocity
if moveControl == "Front" then
initializingVelocity = gunModule.plrObject.hrp.CFrame.LookVector * gunModule.rollingConfig.rollSpeed
elseif moveControl == "Back" then
initializingVelocity = -(gunModule.plrObject.hrp.CFrame.LookVector) * gunModule.rollingConfig.rollSpeed
elseif moveControl == "Right" then
initializingVelocity = gunModule.plrObject.hrp.CFrame.RightVector * gunModule.rollingConfig.rollSpeed
elseif moveControl == "Left" then
initializingVelocity = -(gunModule.plrObject.hrp.CFrame.RightVector) * gunModule.rollingConfig.rollSpeed
end
if not initializingVelocity then
linearVelocity.VectorVelocity = gunModule.plrObject.hrp.CFrame.LookVector * gunModule.rollingConfig.rollSpeed
else
linearVelocity.VectorVelocity = initializingVelocity
end
linearVelocity.Attachment0 = Instance.new("Attachment", gunModule.plrObject.hrp)
linearVelocity.Parent = gunModule.plrObject.hrp
task.wait(gunModule.rollingConfig.rollDuration)
linearVelocity:Destroy()
task.wait(gunModule.rollingConfig.rollCooldown)
gunModule.rollingConfig.isRolling = false
end
end
function gunModule.checkRayRoll()
if not gunModule.plrObject.Plr then return end
if not gunModule.plrObject.hrp or not gunModule.plrObject.camera then return false end
if not gunModule.plrObject.char:FindFirstChildOfClass("Tool") then return false end
local rayStart = gunModule.plrObject.hrp.Position
local rayDirection = gunModule.plrObject.hrp.CFrame.LookVector * 5
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {gunModule.plrObject.char}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
local raycastResult = workspace:Raycast(rayStart, rayDirection, raycastParams)
if raycastResult then
local distanceToHit = (raycastResult.Position - gunModule.plrObject.hrp.Position).Magnitude
local rollThreshold = 4
if distanceToHit <= rollThreshold then
return false
end
end
return true
end
function gunModule.continouscheckRoll()
gunModule.rollingConfig.canRoll = gunModule.checkRayRoll()
end
function gunModule.inputBegan(input, isTyping)
if isTyping or not gunModule.state.ExpectingInput then return end
if input.UserInputType == Enum.UserInputType.MouseButton1 and gunModule.state.playerMouse ~= nil then
gunModule.state.isMouse1down = true
task.wait(gunModule.gunConfig.gunCooldown.Value)
gunModule.state.isMouse1down = false
elseif input.UserInputType == Enum.UserInputType.MouseButton2 and gunModule.state.playerMouse ~= nil then
gunModule.state.isMouse2down = true
gunModule.Scoping(true)
elseif input.KeyCode == Enum.KeyCode.Space and gunModule.state.playerMouse ~= nil then
if gunModule.scopeState.IsScoping == true then
if uis:IsKeyDown(Enum.KeyCode.A) then
rollMovementControl = "Left"
elseif uis:IsKeyDown(Enum.KeyCode.D) then
rollMovementControl = "Right"
elseif uis:IsKeyDown(Enum.KeyCode.S) then
rollMovementControl = "Back"
elseif uis:IsKeyDown(Enum.KeyCode.W) then
rollMovementControl = "Front"
end
gunModule.rollMovement(rollMovementControl)
end
elseif input.KeyCode == Enum.KeyCode.LeftShift and gunModule.state.playerMouse ~= nil then
gunModule.gunEvents.sprintEvent:FireServer(true)
elseif input.KeyCode == Enum.KeyCode.R and gunModule.state.playerMouse ~= nil then
if gunModule.gunConfig.isReloading.Value == false then
gunModule.gunEvents.firingEvent:FireServer(nil,true)
end
end
end
function gunModule.inputEnded(input, isTyping)
if isTyping or not gunModule.state.ExpectingInput then return end
if input.UserInputType == Enum.UserInputType.MouseButton2 and gunModule.state.playerMouse ~= nil then
gunModule.state.isMouse2down = false
gunModule.Scoping(false)
elseif input.KeyCode == Enum.KeyCode.LeftShift and gunModule.state.playerMouse ~= nil then
gunModule.gunEvents.sprintEvent:FireServer(false)
end
end
function gunModule.onEquip(mouse)
gunModule.state.playerMouse = mouse
gunModule.state.ExpectingInput = true
gunModule.state.isMouse1down = false
gunModule.state.isMouse2down = false
gunModule.UIHandler()
end
function gunModule.unEquipped()
gunModule.state.playerMouse = nil
gunModule.state.ExpectingInput = false
gunModule.state.isMouse1down = false
gunModule.state.isMouse2down = false
gunModule.unUI()
end
runService.RenderStepped:Connect(function()
if gunModule.scopeState.IsScoping then
print("The scoping is enabled")
else
warn("Scoping is disabled")
end
end)
return gunModule
And here is the local script where I call the activation
local gun = script.Parent
local uis = game:GetService('UserInputService')
local runService = game:GetService("RunService")
local RS = game:GetService("ReplicatedStorage")
local Modules = RS:WaitForChild("1v1"):WaitForChild("Modules")
local gunModule = require(Modules:WaitForChild("gunModule"))
local configMovement = gun:WaitForChild("ConfigMovement")
local configSystem = gun:WaitForChild("Config")
local maxBullet = configSystem.MaxBullet
local currentBullet = configSystem.currentBullet
local isReloading = configSystem.isReloading
local cooldown = configSystem.Cooldown
local Sounds = gun:WaitForChild("Sounds")
local reload = Sounds:WaitForChild("Reload")
local UIFolder = gun:WaitForChild("UI")
local EventsFolder = gun:WaitForChild("Events")
local animationFolder = gun:WaitForChild("Animation")
local gunUI = UIFolder:WaitForChild("GunUI")
local hitMarkUI = UIFolder:WaitForChild("HitMarkUI")
local sprintEvent = EventsFolder:WaitForChild("Sprint")
local fireEvent = EventsFolder:WaitForChild("Sprint")
local scopeIdleAnim = animationFolder:WaitForChild("ScopedIDLE")
local rollAnim = animationFolder:WaitForChild("Roll")
local cooldown = configSystem.Cooldown
local function loadData()
gunModule.plrObject.Plr = game:GetService("Players").LocalPlayer
gunModule.plrObject.char = gunModule.plrObject.Plr.Character or gunModule.plrObject.Plr.CharacterAdded:Wait()
gunModule.plrObject.hrp = gunModule.plrObject.char:WaitForChild("HumanoidRootPart")
gunModule.plrObject.hum = gunModule.plrObject.char:WaitForChild("Humanoid")
gunModule.plrObject.camera = workspace.CurrentCamera
gunModule.plrObject.PlrGui = gunModule.plrObject.Plr:WaitForChild("PlayerGui")
gunModule.MobileUI.mobileAssistant = gunModule.plrObject.PlrGui:WaitForChild("MobileAssistant")
gunModule.MobileUI.crossHairMobile = gunModule.MobileUI.mobileAssistant:WaitForChild("CrossHair")
gunModule.MobileUI.reloadMobile = gunModule.MobileUI.mobileAssistant:WaitForChild("Reload")
gunModule.MobileUI.FireMobile = gunModule.MobileUI.mobileAssistant:WaitForChild("Firing")
gunModule.MobileUI.rollMobile = gunModule.MobileUI.mobileAssistant:WaitForChild("Roll")
gunModule.MobileUI.sprintMobile = gunModule.MobileUI.mobileAssistant:WaitForChild("Sprint")
gunModule.rollingConfig.isRolling = false
gunModule.rollingConfig.rollSpeed = configMovement.rollSpeed.Value
gunModule.rollingConfig.rollDuration = 0.3
gunModule.rollingConfig.rollAnim = gunModule.plrObject.hum:LoadAnimation(rollAnim)
gunModule.rollingConfig.rollCooldown = 2
gunModule.gunConfig.GunUI = gunUI
gunModule.gunConfig.isReloading = isReloading
gunModule.gunConfig.firingEvent = fireEvent
gunModule.gunConfig.maxBullet = maxBullet
gunModule.gunConfig.currentBullet = currentBullet
gunModule.gunConfig.gunCooldown = cooldown
gunModule.gunEvents.sprintEvent = sprintEvent
gunModule.gunEvents.firingEvent = fireEvent
gunModule.scopeState.scopeIdleAnim = gunModule.plrObject.hum:LoadAnimation(scopeIdleAnim)
end
loadData()
uis.InputBegan:Connect(gunModule.inputBegan)
uis.InputEnded:Connect(gunModule.inputEnded)
gun.Equipped:Connect(gunModule.onEquip)
gun.Unequipped:Connect(gunModule.unEquipped)
I tried so many other ways to fix it, i just ran out of options and needed assistance. If anyone has ever experienced an issue like this, please tell me how you fixed it. Thank you so much
Any help is very appreciated.
And also I forgot to mention, whenever I join, there is an untraceable error I can’t define what it is, perhaps anyone knows?