Here is the script:
local tool = script.Parent -- Getting the tool
local player = game:GetService("Players").LocalPlayer -- Getting the player
local mouse = player:GetMouse() -- Getting the mouse
local sound = tool:WaitForChild("Gunfire")
local torso = "" -- Nothing for now.
local reloading = false -- Variable to check if we are currently reloading
local contextActionService = game:GetService("ContextActionService") -- Allows us to cater for Mobile players
local bodytype = nil -- Nil for now but will check whether player is R6 or R15
local difference = 0 -- Difference between position of head and mouse
local replicatedstorage = game:GetService("ReplicatedStorage")
local gungui = tool:WaitForChild("GunGUI")
local bullets = tool:WaitForChild("Bullets")
local reloadtime = 3
-- Remote Events
local equipAnimation = replicatedstorage:WaitForChild("EquipAnimation")
local headshot = replicatedstorage:WaitForChild("Headshot")
local reload = replicatedstorage:WaitForChild("Reload")
local shootevent = replicatedstorage:WaitForChild("ShootEvent")
local unequipanimation = replicatedstorage:WaitForChild("UnequipAnimation")
-- Remote Functions
local checkBodyType = replicatedstorage:WaitForChild("CheckBodyType")
local fetchBulletsLeft = replicatedstorage:WaitForChild("FetchBulletsLeft")
-- Find Body Type
function findBodyType() -- Used to determine whether a player is R6 or R15
bodytype = checkBodyType:InvokeServer(tool) -- Invoking the Remotefunction to do a check on the server
print(bodytype)
end
-- Reloading function
function reloadd()
reloading = true
reloadd:FireServer(tool.reload)
mouse.Icon = "http:www.roblox.com/asset?id=936489163"
player.PlayerGui:WaitForChild("GunGUI").Bullets.Text = "Reloading!"
wait(reloadtime)
bullets.Value = 6
player.PlayerGui:WaitForChild("GunGUI").Bullets.Text = "Bullets: "..bullets.Value
mouse.Icon = "http://www.roblox.com/asset?id=936803874"
equipAnimation:FireServer(tool.shoot)
reloading = false
end
-- When the tool is equipped, the following event will run
tool.Equipped:Connect(function(mouse) -- We are cloning the Gun GUI into the player's PlayerGUI
gungui:Clone().Parent = player.PlayerGui -- We are cdloning the Gun GUI into the player's PlayerGUI
findBodyType() -- Calling the function above to check the body type.
equipAnimation:FireServer(tool.shoot) -- Calling the equip animation remote event so that the server can play the animation
mouse.Icon = "http://www.roblox.com/asset?id=936803874"
mouse.Button1Down:Connect(function()
if bullets.Value <= 0 or reloading == true then
-- Don't do anything
else
local head = workspace[player.Name].Head.CFrame.lookVector
local mouse = CFrame.new(workspace[player.Name].Head.Position,mouse.Hit.p).lookVector
difference = (head-mouse)
local ray = Ray.new(tool.Tip.CFrame.p, (player:GetMouse().Hit.p - tool.Tip.CFrame.p).unit*300)
local part,position = workspace:FindPartOnRay(ray,player.Character,false,true)
sound:Play()
if difference.magnitude <1.33 then
shootevent:FireServer(tool,position,part)
bullets.Value = bullets.Value - 1
end
end
end)
local reloadMobileButton = contextActionService:BindAction("ReloadBtn",reload,true,"r")
contextActionService:SetPosition("ReloadBtn",UDim2.new(0.72,-25,0.20,-25))
contextActionService:SetImage("ReloadBtn","http://www.roblox.com/asset/?id=10952419")
end)
tool.Unequipped:Connect(function()
mouse.Icon = ""
unequipanimation:FireServer(tool.shoot)
player.PlayerGui.GunGUI:Destroy()
contextActionService:UnbindAction("ReloadBtn")
end)
headshot.OnClientEvent:Connect(function()
player.PlayerGui.GunGUI.Headshot:TweenPosition(UDim2.new(0.5,-100,0.5,-25), "Out","Quint",0.3)
wait(1.5)
player.PlayerGui.GunGUI.Headshot:TweenPosition(UDim2.new(-1,0,0.5,-25), "In","Quint",0.4)
wait(0.5)
player.PlayerGui.GunGUI.Headshot.Position = UDim2.new(1.5,0,0.5,-25)
end)
