Was making a simple gun and not sure what really went wrong here, the bullets shoot from both guns, even if the gun is unequipped
Code
Client
local Tool = script.Parent
local player = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")
local Event = game.ReplicatedStorage:WaitForChild("MainEvent")
local Bullet = workspace.Ignored.ClientBullet
local Debris = game:GetService("Debris")
local IsUsing = false
-- For Easier Changing --
local BulletSize = 0.2
local BulletColor = Color3.new(1,1,0)
local RayLength = 300
-------------------------
local Params = RaycastParams.new()
Params.FilterDescendantsInstances = {}
Params.FilterType = Enum.RaycastFilterType.Blacklist
local function OnEquipped()
local C = {}
local Holder = Tool.Parent
table.insert(C, Holder)
for i, v in ipairs(workspace.Ignored:GetChildren()) do
table.insert(C, v)
end
Params.FilterDescendantsInstances = C
end
local function BulletGen(ray)
local Rv = ray.Direction * RayLength
local Raycast = workspace:Raycast(ray.Origin, Rv, Params)
local HitPos = if Raycast then Raycast.Position else ray.Origin + Rv
local ShootFrom = Tool.Handle.Attachment.WorldPosition
local AimAt = HitPos - ShootFrom
local HitDetection = workspace:Raycast(ShootFrom, AimAt * 2, Params)
local ThingHit = if HitDetection then HitDetection.Instance else nil
local PosHit = if HitDetection then HitDetection.Position else HitPos
local D = Bullet:Clone()
D.Color = BulletColor
D.Size = Vector3.new(BulletSize, BulletSize, (ShootFrom - PosHit).Magnitude)
D.CFrame = CFrame.lookAt(ShootFrom:Lerp(PosHit, .5), PosHit)
D.Parent = workspace.Ignored
Debris:AddItem(D, .05)
end
local function ClientBullet(From, To)
local ReplicatedBullet = Bullet:Clone()
ReplicatedBullet.Color = BulletColor
ReplicatedBullet.Size = Vector3.new(BulletSize, BulletSize, (From - To).Magnitude)
ReplicatedBullet.CFrame = CFrame.lookAt(From:Lerp(To, .5), To)
ReplicatedBullet.Parent = workspace.Ignored
Debris:AddItem(ReplicatedBullet, .05)
end
Tool.Activated:Connect(function()
IsUsing = true
end)
Tool.Deactivated:Connect(function()
IsUsing = false
end)
game:GetService("RunService").Stepped:Connect(function()
if IsUsing == true then
local mouse = UIS:GetMouseLocation()
local ray = workspace.CurrentCamera:ViewportPointToRay(mouse.X, mouse.Y)
Event:FireServer(ray)
BulletGen(ray)
end
end)
script.Parent.Equipped:Connect(OnEquipped)
Event.OnClientEvent:Connect(ClientBullet)
Server
local Tool = script.Parent
local Event = game.ReplicatedStorage.MainEvent
local Params = RaycastParams.new()
local debounce = false
Params.FilterDescendantsInstances = {}
Params.FilterType = Enum.RaycastFilterType.Blacklist
local function OnEquipped()
local C = {}
local Holder = Tool.Parent
table.insert(C, Holder)
for i, v in ipairs(workspace.Ignored:GetChildren()) do
table.insert(C, v)
end
Params.FilterDescendantsInstances = C
end
local function Shoot(Player, ray)
local Rv = ray.Direction * 300
local Raycast = workspace:Raycast(ray.Origin, Rv, Params)
local HitPos = if Raycast then Raycast.Position else ray.Origin + Rv
local ShootFrom = Tool.Handle.Attachment.WorldPosition
local AimAt = HitPos - ShootFrom
local HitDetection = workspace:Raycast(ShootFrom, AimAt * 2, Params)
local ThingHit = if HitDetection then HitDetection.Instance else nil
local PosHit = if HitDetection then HitDetection.Position else HitPos
if ThingHit then
print(ThingHit)
end
for i, v in ipairs(game.Players:GetPlayers()) do
if v ~= Player and v then
Event:FireClient(v, ShootFrom, PosHit)
end
end
end
Event.OnServerEvent:Connect(Shoot)
Tool.Equipped:Connect(OnEquipped)