Hello! I’m trying to make a gun that has a delayed shot. When the user presses a button, then it waits a bit while playing a sound, then it shoots where the mouse currently is… I have been trying to make this for a while but I have no idea on how do delay the raycast. Here is my current script:
Local script
local Tool = script.Parent
local Handle = Tool:WaitForChild("Handle")
local Remote = Tool:WaitForChild("Remote")
local Mouse = nil
local Active = false
Tool.Equipped:Connect(function(m)
Mouse = m
Mouse.Icon = "rbxasset://textures/GunCursor.png"
end)
Tool.Unequipped:Connect(function()
Active = false
Mouse = nil
end)
Tool.Activated:Connect(function()
if Active == true then return end
Active = true
repeat
wait()
local Target = Mouse.Target
if Target ~= nil then
if Target.Parent:IsA("Accessory") or Target.Parent:IsA("Tool") or Target.Transparency >= 1 then
Mouse.TargetFilter = Mouse.Target
end
end
Remote:FireServer(Mouse.Hit.p)
until Active == false
end)
Tool.Deactivated:Connect(function()
if Active == false then return end
Active = false
end)
Server sided script
local Tool = script.Parent
local Handle = Tool:WaitForChild("Handle")
local Remote = Tool:WaitForChild("Remote")
local ProjectilePosition = Handle:WaitForChild("ProjectilePosition")
local MuzzlePE = ProjectilePosition:WaitForChild("MuzzlePE")
local Smoke = script:WaitForChild("SmokePE")
local Character
local Player
local Humanoid
local GunshotSFX = Handle:WaitForChild("GunshotSFX")
local Music = Handle:WaitForChild("Music")
local Debounce = false
local IG = {}
function Equipped()
Character = Tool.Parent
Player = game.Players:GetPlayerFromCharacter(Character)
Humanoid = Character:WaitForChild("Humanoid")
table.insert(IG,Character)
Music:Play()
end
function Unequipped()
Music:Stop()
end
function Raycast(MousePosition)
local RayLaser = Ray.new(ProjectilePosition.WorldCFrame.Position, (MousePosition - ProjectilePosition.WorldCFrame.Position).unit * 300)
local Hit, Position = game.Workspace:FindPartOnRayWithIgnoreList(RayLaser, IG)
if Hit ~= nil then
if Hit.Parent:IsA("Accessory") or Hit.Transparency >= 1 or Hit.Parent:IsA("Tool") then
table.insert(IG,Hit)
return Raycast(MousePosition)
end
end
local BulletHole = Instance.new("Part")
BulletHole.BrickColor = BrickColor.new("Really black")
BulletHole.Anchored = true
BulletHole.CanCollide = false
BulletHole.Massless = true
BulletHole.Shape = Enum.PartType.Ball
BulletHole.Size = Vector3.new(.25,.25,.25)
BulletHole.CFrame = CFrame.new(Position,ProjectilePosition.WorldPosition)
BulletHole.Name = "BulletHole"
BulletHole.Parent = game.Workspace
if Hit ~= nil then
local Weld = Instance.new("WeldConstraint")
Weld.Parent = BulletHole
Weld.Part0 = BulletHole
Weld.Part1 = Hit
BulletHole.Anchored = false
local CloneSmoke = Smoke:Clone()
CloneSmoke.Parent = BulletHole
CloneSmoke.Color = ColorSequence.new(Hit.Color)
CloneSmoke:Emit(15)
end
game.Debris:AddItem(BulletHole,5)
local Attachment = Instance.new("Attachment")
Attachment.Name = "A1"
Attachment.Parent = BulletHole
local Beam = Instance.new("Beam")
Beam.Width0 = .25
Beam.Width1 = .25
Beam.FaceCamera = true
Beam.Color = ColorSequence.new(Color3.fromRGB(255, 255, 127))
Beam.Parent = ProjectilePosition
Beam.Attachment0 = ProjectilePosition
Beam.Attachment1 = Attachment
game.Debris:AddItem(Beam,.05)
if Hit ~= nil then
local EnemyHumanoid = Hit.Parent:FindFirstChild("Humanoid")
if EnemyHumanoid ~= nil then
if BulletHole ~= nil then
local Attachment1 = Instance.new("Attachment")
Attachment1.Name = "A1"
Attachment1.Parent = Hit
Attachment1.WorldPosition = Attachment.WorldPosition
Beam.Attachment1 = Attachment1
game.Debris:AddItem(Attachment1,1)
BulletHole:Destroy()
end
local EnemyPlayer = game.Players:GetPlayerFromCharacter(EnemyHumanoid.Parent)
if EnemyPlayer and not Player.Neutral and not EnemyPlayer.Neutral and Player.TeamColor == EnemyPlayer.TeamColor then
return
end
EnemyHumanoid:TakeDamage(99999)
end
end
end
Remote.OnServerEvent:Connect(function(FiredPlayer,MousePosition)
if Humanoid.Health == 0 then return end
if FiredPlayer ~= Player then return end
if Debounce == true then return end
Debounce = true
Humanoid.WalkSpeed = 5
script.Parent.Handle.Sound:Play()
wait(3)
script.Parent.Handle.Sound:Stop()
MuzzlePE:Emit(5)
local GunshotSFXClone = GunshotSFX:Clone()
GunshotSFXClone.Parent = ProjectilePosition
GunshotSFXClone:Play()
game.Debris:AddItem(GunshotSFXClone, 2)
local PointLight = ProjectilePosition.PointLight:Clone()
PointLight.Parent = ProjectilePosition
PointLight.Enabled = true
game.Debris:AddItem(PointLight,.05)
Raycast(MousePosition)
Humanoid.WalkSpeed = 16
wait(10)
script.Parent:Destroy()
Debounce = false
end)
Tool.Equipped:Connect(Equipped)
Tool.Unequipped:Connect(Unequipped)
``````lua
local Tool = script.Parent
local Handle = Tool:WaitForChild("Handle")
local Remote = Tool:WaitForChild("Remote")
local ProjectilePosition = Handle:WaitForChild("ProjectilePosition")
local MuzzlePE = ProjectilePosition:WaitForChild("MuzzlePE")
local Smoke = script:WaitForChild("SmokePE")
local Character
local Player
local Humanoid
local GunshotSFX = Handle:WaitForChild("GunshotSFX")
local Music = Handle:WaitForChild("Music")
local Debounce = false
local IG = {}
function Equipped()
Character = Tool.Parent
Player = game.Players:GetPlayerFromCharacter(Character)
Humanoid = Character:WaitForChild("Humanoid")
table.insert(IG,Character)
Music:Play()
end
function Unequipped()
Music:Stop()
end
function Raycast(MousePosition)
local RayLaser = Ray.new(ProjectilePosition.WorldCFrame.Position, (MousePosition - ProjectilePosition.WorldCFrame.Position).unit * 300)
local Hit, Position = game.Workspace:FindPartOnRayWithIgnoreList(RayLaser, IG)
if Hit ~= nil then
if Hit.Parent:IsA("Accessory") or Hit.Transparency >= 1 or Hit.Parent:IsA("Tool") then
table.insert(IG,Hit)
return Raycast(MousePosition)
end
end
local BulletHole = Instance.new("Part")
BulletHole.BrickColor = BrickColor.new("Really black")
BulletHole.Anchored = true
BulletHole.CanCollide = false
BulletHole.Massless = true
BulletHole.Shape = Enum.PartType.Ball
BulletHole.Size = Vector3.new(.25,.25,.25)
BulletHole.CFrame = CFrame.new(Position,ProjectilePosition.WorldPosition)
BulletHole.Name = "BulletHole"
BulletHole.Parent = game.Workspace
if Hit ~= nil then
local Weld = Instance.new("WeldConstraint")
Weld.Parent = BulletHole
Weld.Part0 = BulletHole
Weld.Part1 = Hit
BulletHole.Anchored = false
local CloneSmoke = Smoke:Clone()
CloneSmoke.Parent = BulletHole
CloneSmoke.Color = ColorSequence.new(Hit.Color)
CloneSmoke:Emit(15)
end
game.Debris:AddItem(BulletHole,5)
local Attachment = Instance.new("Attachment")
Attachment.Name = "A1"
Attachment.Parent = BulletHole
local Beam = Instance.new("Beam")
Beam.Width0 = .25
Beam.Width1 = .25
Beam.FaceCamera = true
Beam.Color = ColorSequence.new(Color3.fromRGB(255, 255, 127))
Beam.Parent = ProjectilePosition
Beam.Attachment0 = ProjectilePosition
Beam.Attachment1 = Attachment
game.Debris:AddItem(Beam,.05)
if Hit ~= nil then
local EnemyHumanoid = Hit.Parent:FindFirstChild("Humanoid")
if EnemyHumanoid ~= nil then
if BulletHole ~= nil then
local Attachment1 = Instance.new("Attachment")
Attachment1.Name = "A1"
Attachment1.Parent = Hit
Attachment1.WorldPosition = Attachment.WorldPosition
Beam.Attachment1 = Attachment1
game.Debris:AddItem(Attachment1,1)
BulletHole:Destroy()
end
local EnemyPlayer = game.Players:GetPlayerFromCharacter(EnemyHumanoid.Parent)
if EnemyPlayer and not Player.Neutral and not EnemyPlayer.Neutral and Player.TeamColor == EnemyPlayer.TeamColor then
return
end
EnemyHumanoid:TakeDamage(99999)
end
end
end
Remote.OnServerEvent:Connect(function(FiredPlayer,MousePosition)
if Humanoid.Health == 0 then return end
if FiredPlayer ~= Player then return end
if Debounce == true then return end
Debounce = true
Humanoid.WalkSpeed = 5
script.Parent.Handle.Sound:Play()
wait(3)
script.Parent.Handle.Sound:Stop()
MuzzlePE:Emit(5)
local GunshotSFXClone = GunshotSFX:Clone()
GunshotSFXClone.Parent = ProjectilePosition
GunshotSFXClone:Play()
game.Debris:AddItem(GunshotSFXClone, 2)
local PointLight = ProjectilePosition.PointLight:Clone()
PointLight.Parent = ProjectilePosition
PointLight.Enabled = true
game.Debris:AddItem(PointLight,.05)
Raycast(MousePosition)
Humanoid.WalkSpeed = 16
wait(10)
script.Parent:Destroy()
Debounce = false
end)
Tool.Equipped:Connect(Equipped)
Tool.Unequipped:Connect(Unequipped)
As you see I’m working with remotes N stuff. The current shoots where the player had the Mouse the moment they clicked. But I want it to shoot where their mouse is after the delay. Can anyone help?