I was working on a gun for my game and when you pull out the gun the mouse is locked on the screen, then when un-equip the gun the mouse become unlocked. My game just freezes then it says script timeout can anyone fix it? Script:
local gun = script.Parent
local player = game.Players.LocalPlayer
local Character = game.Players.LocalPlayer.Character
if not Character or not Character.Parent then
Character = game.Players.LocalPlayer.CharacterAdded:Wait()
end
local humanoid = Character:WaitForChild("Humanoid")
local uis = game:GetService("UserInputService")
local animation = Instance.new("Animation")
animation.Name = "Shoot"
animation.Parent = script.Parent
animation.AnimationId = "http://www.roblox.com/asset/?id=6688606392"
local animtrack = humanoid:LoadAnimation(animation)
local gun_shot = gun.Handle['Gun Shot']
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local remoteEvent = ReplicatedStorage:WaitForChild('ShotEvent')
local shooting = false
local equipped = false
local mouse = player:GetMouse()
local mouseConnection
function UnlockMouse()
uis.MouseBehavior = Enum.MouseBehavior.Default
end
function lockMouse()
uis.MouseBehavior = Enum.MouseBehavior.LockCenter
end
gun.Equipped:Connect(function()
equipped = true
repeat until equipped == false
lockMouse()
end)
mouse.Icon = 'rbxassetid://117431027'
mouseConnection = mouse.Button1Down:Connect(function()
shooting = true
while shooting and equipped do
remoteEvent:FireServer(gun.Handle.Position, gun.Handle.Orientation, mouse.Hit.p)
animtrack:Play()
gun_shot:Play()
mouse.Button1Up:Connect(function()
animtrack:Stop()
shooting = false
end)
wait(0.1)
end
end)
gun.Unequipped:Connect(function()
equipped = false
UnlockMouse()
mouseConnection:Disconnect()
end)
local uis = game:GetService("UserInputService")
local Runanim = Instance.new("Animation")
local NormalRunanim = Instance.new("Animation")
Runanim.AnimationId = "https://www.roblox.com/asset/?id=6689165237"
NormalRunanim.AnimationId = "https://www.roblox.com/asset/?id=6690951389"
local Ranim = humanoid:LoadAnimation(Runanim)
local NRanim = humanoid:LoadAnimation(NormalRunanim)
repeat wait() until game.Players.LocalPlayer
m = game.Players.LocalPlayer:GetMouse()
m.KeyDown:connect(function(key)
if key == "0" and shooting == false and equipped == true then --"Shift to run" 0 == shift
print("Gun Running")
Ranim:Play()
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 32
end
if key == "0" and shooting == false and equipped == false then
print("Normal running")
NRanim:play()
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 32--change "25" to your speed you want
end
end)
m.KeyUp:connect(function(key)
if key == "0" then
Ranim:Stop()
NRanim:Stop()
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16 --change "16" to your speed you want when you stop running
end
end)
if shooting == true then
Ranim:Stop()
NRanim:Stop()
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
end
