Hello,
I am currently unsatisfied with my code. I have created a LocalScript using Functions and UserInputType, etc, and created a bunch of spaghetti code which is very hard for my eyes to handle. On the other hand, it does get the job done, but is very hard to understand at times .
- Improvements, I want to upgrade the Organization and purpose of using functions. The script shines runs off of just ONE Module Script and can get VERY HARD TO UNDERSTAND .
- Maids and Classes, I would like to make everything depend off of classes using Module Scripts, and easier to configure.
- It is not the best Filtering Enabled script, it needs to be improved greatly and is barely FE.
Code
-- Services
local UserInputService = game:GetService("UserInputService")
-- Main Variables
local tool = script.Parent
local aiming = false
local meleeing = false
local tagged = nil
local cooldown = false
local reloading = false
local debounce = false
local Player = game.Players.LocalPlayer
local ExpectingInput = false
local MouseEvent = tool:WaitForChild("MouseEvent")
local FirePointObject = tool.Emitter:WaitForChild("GunFirePoint")
local followMouse = false
local recoil = 0
local firing = false
local Handle = tool.Handle
-- Configuration
local animPath = game.Lighting.Animations.Rifle
local rps = 0.0666666667000
local reloadTime = tool.Handle.reload.TimeLength
local headshotDamage = 25
local defaultDamage = 17
local reloadRequirements = "and ammo.Value < ammo.MaxValue and not reloading and heldammo.Value > 0"
-- Functions
function reload()
reloading = true
tool.Handle.reload:Play()
rld:Play(0.25)
tool.Mag.Transparency = 1
wait(reloadTime)
tool.Mag.Transparency = 0
reloading = false
heldammo.Value = heldammo.Value - 1
ammo.Value = ammo.MaxValue
end
function enable()
aim:Play(0.25)
tool.CLIENT_SCRIPT.Disabled = false
tool.CLIENT_TORSOSCRIPT.Return:Fire(true)
hold:Stop()
print('Animation success.')
tool.GripForward = Vector3.new(-0.1, 0, -0.9)
tool.GripRight = Vector3.new(0.9, 0, -0.1)
aiming = true
end
function disable()
aim:Stop()
tool.CLIENT_SCRIPT.Disabled = true
tool.CLIENT_TORSOSCRIPT.Return:Fire(false)
hold:Play(0.25)
print('Animation success.')
tool.GripForward = Vector3.new(0, 0, -1)
tool.GripRight = Vector3.new(1, 0, 0)
aiming = false
end
function shoot(hitPart)
while firing and ammo.Value > 0 and aiming do
recoil = recoil + .5
ammo.Value = ammo.Value - 1
fire:Play()
aim:Play(0.25)
hold:Stop()
end
end
function unEquip()
tool.CLIENT_SCRIPT.Disabled = true
aim:Stop()
hold:Stop()
melee:Stop()
fire:Stop()
rld:Stop()
ExpectingInput = false
aiming = false
end
-- Main Script
tool.Equipped:connect(function(mouse)
local character = tool.Parent
local hum = character.Humanoid
local Player = game:GetService("Players").LocalPlayer
ExpectingInput = true
local rlMouse = Player:GetMouse()
ammo = script.Parent.Ammo
heldammo = script.Parent.Stock
mouse.Icon = "http://www.roblox.com/asset/?id=106491038"
hold = hum:LoadAnimation(animPath.idle)
aim = hum:LoadAnimation(animPath.aim)
fire = hum:LoadAnimation(animPath.fire)
melee = hum:LoadAnimation(animPath.melee)
rld = hum:LoadAnimation(animPath.reload)
hold:Play(0.25)
debounce = true
UserInputService.InputBegan:Connect(function (input, gameHandledEvent)
if gameHandledEvent or not ExpectingInput then
return
end
if input.UserInputType == Enum.KeyCode == Enum.KeyCode.R and ammo.Value < ammo.MaxValue and not reloading then
reload()
if input.UserInputType == Enum.KeyCode == Enum.KeyCode.E and not meleeing and not reloading then
if aiming == false then
-- Enabled
enable()
elseif aiming == true then
-- Disabled
disable()
end
end
if input.UserInputType == Enum.UserInputType.MouseButton1 then
if aiming and not meleeing and not cooldown and ammo.Value > 0 and not reloading then
local FireDirection = (mouse.Hit.p - FirePointObject.WorldPosition).Unit
aim:Play(0.25)
hold:Stop()
tool.CLIENT_SCRIPT.Disabled = false
firing = true
cooldown = true
shoot()
MouseEvent:FireServer(FireDirection)
wait(.01)
cooldown = false
fire:Stop()
elseif not meleeing and not aiming and not reloading and not firing then
end
end
end
end)
end)
tool.Unequipped:connect(function()
unEquip()
end)
Thanks,
Rook.