Skip to content

Instantly share code, notes, and snippets.

@B-Ricey763
Last active May 19, 2021 19:10
Show Gist options
  • Select an option

  • Save B-Ricey763/28c4b58f2a1be4f5abb94cf14fe6febe to your computer and use it in GitHub Desktop.

Select an option

Save B-Ricey763/28c4b58f2a1be4f5abb94cf14fe6febe to your computer and use it in GitHub Desktop.
Shotgun Code for Roblox Studio
Code for use in Roblox Studio Shotgun (https://youtu.be/sBdV4mimlYs)
local UserInputService = game:GetService("UserInputService")
local fireEvent = script.Parent:WaitForChild("FireEvent")
local camera = workspace.CurrentCamera
local tool = script.Parent
tool.Activated:Connect(function()
local pos = UserInputService:GetMouseLocation()
local ray = camera:ViewportPointToRay(pos.X, pos.Y)
fireEvent:FireServer(ray)
end)
local tool = script.Parent
local fireEvent = tool.FireEvent
local FastCast = require(tool.FastCastRedux)
local PartCache = require(tool.PartCache)
local pellet = game:GetService("ServerStorage").Pellet
local pelletsFolder = workspace.PelletsFolder
local firePoint = tool.Handle.FirePoint
FastCast.VisualizeCasts = false
local MAX_DIST = 500
local MIN_SPREAD = 1
local MAX_SPREAD = 4
local caster = FastCast.new()
local provider = PartCache.new(pellet, 100, pelletsFolder)
local castParams = RaycastParams.new()
castParams.FilterType = Enum.RaycastFilterType.Blacklist
local behavior = FastCast.newBehavior()
behavior.RaycastParams = castParams
behavior.CosmeticBulletProvider = provider
behavior.CosmeticBulletContainer = pelletsFolder
behavior.Acceleration = Vector3.new(0, -workspace.Gravity, 0)
local function GetMousePos(unitRay)
local ori, dir = unitRay.Origin, unitRay.Direction * MAX_DIST
local result = workspace:Raycast(ori, dir, castParams)
return result and result.Position or ori + dir
end
local function Fire(direction)
local directionCF = CFrame.new(Vector3.new(), direction)
local spreadDirection = CFrame.fromOrientation(0, 0, math.random(0, math.pi * 2))
local spreadAngle = CFrame.fromOrientation(math.rad(math.random(MIN_SPREAD, MAX_SPREAD)), 0, 0)
local direction = (directionCF * spreadDirection * spreadAngle).LookVector
caster:Fire(firePoint.WorldPosition, direction, 1000, behavior)
end
tool.Equipped:Connect(function()
castParams.FilterDescendantsInstances = {tool.Parent}
end)
fireEvent.OnServerEvent:Connect(function(player, ray)
if tool.Parent:IsA("Backpack") then return end
local pos = GetMousePos(ray)
local direction = (pos - firePoint.WorldPosition).Unit
for i = 1, 10 do
Fire(direction)
end
end)
caster.LengthChanged:Connect(function(cast, lastPoint, dir, displacment, segVel, pellet)
pellet.CFrame = CFrame.lookAt(lastPoint + (dir * displacment), lastPoint)
end)
caster.RayHit:Connect(function(cast, result, segVel, pellet)
if result.Instance then
local character = result.Instance:FindFirstAncestorWhichIsA("Model")
local humanoid = character and character:FindFirstChild("Humanoid")
if humanoid then
humanoid:TakeDamage(10)
provider:ReturnPart(pellet)
else
wait(1)
provider:ReturnPart(pellet)
end
end
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment