Server code: local Tool = script.Parent local Handle = Tool.Handle local MouseEvent = Tool.MouseEvent local CanFire = true local MIN_BULLET_SPREAD_ANGLE = 0 local MAX_BULLET_SPREAD_ANGLE = 0 local FIRE_DELAY = 0.1 local BULLETS_PER_SHOT = 1 local RNG = Random.new() local TAU = math.pi * 2 local VisualizeBulletsEvent = game:GetService("ReplicatedStorage"):WaitForChild("VisualizeBulletsEvent") function Fire(endPoint, startPos) if Tool.Parent:IsA("Backpack") then return end local direction = (endPoint - Handle.Front.Position).Unit local directionalCF = CFrame.new(Vector3.new(), direction) local newDirection = (directionalCF * CFrame.fromOrientation(0, 0, RNG:NextNumber(0, TAU)) * CFrame.fromOrientation(math.rad(RNG:NextNumber(MIN_BULLET_SPREAD_ANGLE, MAX_BULLET_SPREAD_ANGLE)), 0, 0)).LookVector local raycastParams = RaycastParams.new() raycastParams.FilterDescendantsInstances = {script.Parent.Parent} raycastParams.FilterType = Enum.RaycastFilterType.Exclude raycastParams.IgnoreWater = true local raycastResult = workspace:Raycast(startPos, newDirection * 250, raycastParams) if raycastResult then if raycastResult.Distance 0.03 then task.wait(FIRE_DELAY) end CanFire = true end)
local VisualizeBulletsEvent = game:GetService("ReplicatedStorage"):WaitForChild("VisualizeBulletsEvent") local Tween Service = game:GetService("TweenService") local ShotPart = game:GetService("ReplicatedStorage"):WaitForChild("ShotPart") local RunService = game:GetService("RunService") local DebrisService = game:GetService("Debris") local function visualize(startPos, endpos, distance, color)
YO IT WORKS TYSM And how can I add sound effects when the gun shoots? Here's the code For everyone just trying to help :] LocalScript in The gun : local tool = script.Parent local handle = tool.Handle local Players = game:GetService("Players") local MouseEvent = tool:WaitForChild("MouseEvent") local Mouse = Players.LocalPlayer:GetMouse() local ExpectingInput = false local IsMouseDown = false function OnEquipped(playerMouse) Mouse = playerMouse ExpectingInput = true IsMouseDown = false end function OnUnequipped() ExpectingInput = false IsMouseDown = false end function Activated() IsMouseDown = true end function OnDeactivated() IsMouseDown = false end tool.Equipped:Connect(OnEquipped) tool.Unequipped:Connect(OnUnequipped) tool.Activated:Connect(Activated) tool.Deactivated:Connect(OnDeactivated) game:GetService("RunService").Stepped:Connect(function() if IsMouseDown == true then MouseEvent:FireServer(Mouse.Hit.Position, handle.Front.Position) end end) LocalScript in the starterplayerscript : local VisualizeBulletsEvent = game:GetService("ReplicatedStorage"):WaitForChild("VisualizeBulletsEvent") local TweenService = game:GetService("TweenService") local ShotPart = game:GetService("ReplicatedStorage"):WaitForChild("ShotPart") local RunService = game:GetService("RunService") local DebrisService = game:GetService("Debris") local function visualize(startPos, endPos, distance, color) local bullet = ShotPart:Clone() bullet:WaitForChild("Middle"):WaitForChild("Trails"):WaitForChild("Inner").Color = ColorSequence.new(color) bullet:WaitForChild("Middle"):WaitForChild("Trails"):WaitForChild("Outter").Color = ColorSequence.new(color) bullet:WaitForChild("Middle"):WaitForChild("Light").Color = Color3.new(color) bullet.Position = startPos local goal = {} goal.Position = endPos local tweenInfo = TweenInfo.new(distance/500) local tween = TweenService:Create(bullet, tweenInfo, goal) DebrisService:AddItem(bullet, distance/500) bullet.Parent = game.Workspace RunService.RenderStepped:Wait() RunService.RenderStepped:Wait() tween:Play() end VisualizeBulletsEvent.OnClientEvent:Connect(visualize)
Hello, I made the gun successfully the first time (making a zombie game, I made some modifications but other than that everything is working perfectly), but I tried making a pistol now using the same code and parts, and it won't work! I have made sure to make the proper modifications to the scripts to reference the new parts inside the pistol, and I am returning 0 errors in the output. So then i tried just simply duplicating the Rifle I made, and it didn't work! exact same parts and code, no modifications, and it didn't work!!! It seems the way the code is done you can only have one of these guns??? I have tried to make new remote events and a new bullet script to listen for the new remote event to see if this was the problem, and nothing. Another thing I tried was putting "print(print something here)" after lines to make sure they ran, and it when I put a print after my local script fires the event, it prints showing that the code worked, but then the server script never actually hears the event and never runs at all!!! I am at a lost, I will continue to try to fix this and will reply to my own comment if I figure it out myself before a response. Please try to get back to me because this game is looking really good currently and I don't want this to waste too much of my limited time. Thank you so much for the video, and thank you for any potential response!
Interesting. Can you verify that there is a part called 'Front' inside your pistol and that it is positioned where the bullets should come out of and that it is welded to the rest of the gun?
@@Scrip_tix Yeah, there is a part called front, but I've made a new discovery, if I duplicate the assault rifle that is already working, and put it in the starter pack, it works, but if I don't modify a single thing, but instead put the gun in replicated storage, and have a script duplicate the gun and give you the gun, then it doesn't work.
So my idea is, that I am trying to make a pistol as your starting weapon, I got rid of the pistol bc it wasn't working and I am trying to test potential problems using a duplicate of the rifle that I made using this video that actually works. When I put the working rifle in replicated storage, and then once you click play on a gui, duplicate the gun twice and put one in starter gear and one in the players backpack, it doesn't work. It gives you the gun, you can hold it but it wont shoot and won't deal damage. The duplicate of the gun works perfectly fine if I place it in the starter pack though, which might be something to test out, I will update soon if I can find a compromise.
Hey! I figured out the issue! Thank you so much for taking your time to respond to me for absolutely no personal gain, even though I solved it myself, it shows you care a lot about your viewers. I noticed that when I play tested in the current client, that I couldn't see the gun and then remembered that I duplicated the gun and put the gun in starter gear and the starter pack through a local script. Now I put a remote event in and used a regular script, and now it works perfectly!! Thank you for responding and if anyone else somehow has a similar problem, yk what to tell them.
is your tool anchored? if yes,then unanchor it,it's mostly caused by the enforcement to the gravity that the game suggests,meaning if you set your tool anchored,the rotation and position of the tool will be kept,leading you to have this problem
The gun works but I can’t see the bullets, there’s an error in Local Script in Starterplayerscripts about line 10 that says “ColorSequence.new(): table expected.” What does it mean?
@@Scrip_tix local VisualizeBulletsEvent = game:GetService("ReplicatedStorage"):WaitForChild("VisualizeBulletsEvent") local TweenService = game:GetService("TweenService") local ShotPart = game:GetService("ReplicatedStorage"):WaitForChild("ShotPart") local RunService = game:GetService("RunService") local DebrisService = game:GetService("Debris") local function visualize(startPos, endPos, distance, color)
end VisualizeBulletsEvent.OnClientEvent:Connect(visualize) btw now there is another error in the local script of my gun "Handle us not a valid member of Tool "Players.(my absolutely embarrassing user here).Backpack.Pistol" so heres the script for that local Tool = script.Parent local Handle = Tool.Handle local Players = game:GetService("Players") local MouseEvent = Tool:WaitForChild("MouseEvent") local Mouse = Players.LocalPlayer:GetMouse() local ExpectingInput = false local IsMouseDown = false function OnEquipped(playerMouse) Mouse = playerMouse ExpectingInput = true IsMouseDown = false end function OnUnequipped() ExpectingInput = false IsMouseDown = false end function OnActivated() IsMouseDown = true end function OnDeactivated() IsMouseDown = false end Tool.Equipped:Connect(OnEquipped) Tool.Unequipped:Connect(OnUnequipped) Tool.Activated:Connect(OnActivated) Tool.Deactivated:Connect(OnDeactivated)
game:GetService("RunService").Stepped:Connect(function() if IsMouseDown == true then MouseEvent:FireServer(Mouse.Hit.Position, Handle.Front.Position) end end)
@@Scrip_tix the problem isnt showing up anymore for some reason but here's the code local VisualizeBulletsEvent = game:GetService("ReplicatedStorage"):WaitForChild("VisualizeBulletsEvent") local TweenService = game:GetService("TweenService") local ShotPart = game:GetService("ReplicatedStorage"):WaitForChild("ShotPart") local RunService = game:GetService("RunService") local DebrisService = game:GetService("Debris") local function visualize(startPos, endPos, distance, color)
end VisualizeBulletsEvent.OnClientEvent:Connect(visualize) now, a new problem is showing up, "Handle is not a valid member of Tool "Players.(myuser).Backpack.Pistol" - Client - LocalScript:2 (in the gun) so heres the other local script local Tool = script.Parent local Handle = Tool.Handle local Players = game:GetService("Players") local MouseEvent = Tool:WaitForChild("MouseEvent") local Mouse = Players.LocalPlayer:GetMouse() local ExpectingInput = false local IsMouseDown = false function OnEquipped(playerMouse) Mouse = playerMouse ExpectingInput = true IsMouseDown = false end function OnUnequipped() ExpectingInput = false IsMouseDown = false end function OnActivated() IsMouseDown = true end function OnDeactivated() IsMouseDown = false end Tool.Equipped:Connect(OnEquipped) Tool.Unequipped:Connect(OnUnequipped) Tool.Activated:Connect(OnActivated) Tool.Deactivated:Connect(OnDeactivated)
game:GetService("RunService").Stepped:Connect(function() if IsMouseDown == true then MouseEvent:FireServer(Mouse.Hit.Position, Handle.Front.Position) end end)
@@Scrip_tix The problem changed now, it's "Handle is not a valid member of Tool "Player.DxrkShadowVibez.Backpack.Pistol" so heres the local script for that says its line 2 btw local Tool = script.Parent local Handle = Tool.Handle local Players = game:GetService("Players") local MouseEvent = Tool:WaitForChild("MouseEvent") local Mouse = Players.LocalPlayer:GetMouse() local ExpectingInput = false local IsMouseDown = false function OnEquipped(playerMouse) Mouse = playerMouse ExpectingInput = true IsMouseDown = false end function OnUnequipped() ExpectingInput = false IsMouseDown = false end function OnActivated() IsMouseDown = true end function OnDeactivated() IsMouseDown = false end Tool.Equipped:Connect(OnEquipped) Tool.Unequipped:Connect(OnUnequipped) Tool.Activated:Connect(OnActivated) Tool.Deactivated:Connect(OnDeactivated)
game:GetService("RunService").Stepped:Connect(function() if IsMouseDown == true then MouseEvent:FireServer(Mouse.Hit.Position, Handle.Front.Position) end end)
@@Scrip_tix the problem changed to "Handle is not a valid member of Tool Players.DxrkShaadowVibez.Backpack.Pistol" line 2 so heres the local script for the gun local Tool = script.Parent local Handle = Tool.Handle local Players = game:GetService("Players") local MouseEvent = Tool:WaitForChild("MouseEvent") local Mouse = Players.LocalPlayer:GetMouse() local ExpectingInput = false local IsMouseDown = false function OnEquipped(playerMouse) Mouse = playerMouse ExpectingInput = true IsMouseDown = false end function OnUnequipped() ExpectingInput = false IsMouseDown = false end function OnActivated() IsMouseDown = true end function OnDeactivated() IsMouseDown = false end Tool.Equipped:Connect(OnEquipped) Tool.Unequipped:Connect(OnUnequipped) Tool.Activated:Connect(OnActivated) Tool.Deactivated:Connect(OnDeactivated)
game:GetService("RunService").Stepped:Connect(function() if IsMouseDown == true then MouseEvent:FireServer(Mouse.Hit.Position, Handle.Front.Position) end end)
Hey, Script works perfectly fine but I was wondering if you could help me add an integrated system inside the preexisting scripts to play a bullet sound every time a bullet comes out of the gun. I can a write a separate script outside to match the weapons fire delay but once I start adding more guns I think I will run into some issues with timing. Can you help me write something that uses the fire delay variable?
This error looks like it shouldn't be happening i have copied it word for word and i dont know what to do, it is in line 18 for Local Script: local tween =TweenService:Create(bullet, tweenInfo, goal) Error Message 01:27:49.996 Players.Drtrayblox123yea.PlayerScripts.LocalScript:18: attempt to index nil with 'Create' - Studio
@@Scrip_tix local VisualizeBulletsEvent = game:GetService("ReplicatedStorage"):WaitForChild("VisualizeBulletsEvent") local Tween Service = game:GetService("TweenService") local ShotPart = game:GetService("ReplicatedStorage"):WaitForChild("ShotPart") local RunService = game:GetService("RunService") local DebrisService = game:GetService("Debris") local function visualize(startPos, endpos, distance, color)
@@Scrip_tix local VisualizeBulletsEvent = game:GetService("ReplicatedStorage"):WaitForChild("VisualizeBulletsEvent") local Tween Service = game:GetService("TweenService") local ShotPart = game:GetService("ReplicatedStorage"):WaitForChild("ShotPart") local RunService = game:GetService("RunService") local DebrisService = game:GetService("Debris") local function visualize(startPos, endpos, distance, color)
@@Scrip_tix local VisualizeBulletsEvent = game:GetService("ReplicatedStorage"):WaitForChild("VisualizeBulletsEvent") local Tween Service = game:GetService("TweenService") local ShotPart = game:GetService("ReplicatedStorage"):WaitForChild("ShotPart") local RunService = game:GetService("RunService") local DebrisService = game:GetService("Debris") local function visualize(startPos, endpos, distance, color)
Ok i have the pistol working, please ignore my other comments. How would i change the script to make it only fire once with a small cooldown per mouseclick?
Hey its not working! I need help. local Tool = script.Parent local Handle = Tool.Handle local MouseEvent = Tool.MouseEvent local CanFire = true local MIN_BULLET_SPREAD_ANGLE = 0 local MAX_BULLET_SPREAD_ANGLE = 0 local FIRE_DELAY = 0.1 local BULLETS_PER_SHOT = 1 local RNG = Random.new() local TAU = math.pi * 2 local VisualizeBulletsEvent = game:GetService("ReplicatedStorage"):WaitForChild("VisualizeBulletsEvent") function Fire(endPoint, startPos) if Tool.Parent:IsA("Backpack") then return end local direction = (endPoint - Handle.Front.Position).Unit local directionalCF = CFrame.new(Vector3.new(), direction) local newDirection = (directionalCF * CFrame.fromOrientation(0, 0, RNG:NextNumber(0, TAU)) * CFrame.fromOrientation(math.rad(RNG:NextNumber(MIN_BULLET_SPREAD_ANGLE, MAX_BULLET_SPREAD_ANGLE)), 0, 0)).LookVector local raycastParams = RaycastParams.new() raycastParams.FilterDescendantsInstances = {script.Parent.Parent} raycastParams.FilterType = Enum.RaycastFilterType.Exclude raycastParams.IgnoreWater = true local raycastResult = workspace:Raycast(startPos, newDirection * 250, raycastParams) if raycastResult then if raycastResult.Distance 0.03 then task.wait(FIRE_DELAY) end CanFire = true end) i didnt know what to do at the end lol
The raycasting is done on the server side, with a RemoteEvent that fires to tell clients to create the bullet effects, so yes, the shots will appear on everyone else's screens :3
@@Scrip_tix Its not my code, its yours.In the video the shots also go through everything. Im not complaining it works fine, but i was wondering IF you could make a tutorial on how to make the bullet disappear after it touches a part. All my code is the same as your.
@@Scrip_tix Assault Rifle Script local tool = script.Parent local handle = tool.Handle local MouseEvent = tool:WaitForChild("MouseEvent") local CanFire = true local MIN_BULLET_SPREAD_ANGLE = 0 local MAX_BULLET_SPREAD_ANGLE = 0 local FIRE_DELAY = 0.25 local BULLETS_PER_SHOT = 1 local RNG = Random.new() local TAU = math.pi * 2 local VisualizeBulletsEvent = game:GetService("ReplicatedStorage"):WaitForChild("VisualizeBulletsEvent") function Fire(endpoint, starPos)
if tool.Parent:IsA("BackPack") then return end
local direction = (endpoint - handle.Front.Position).Unit local direcitonCF = CFrame.new(Vector3.new(), direction) local newDirection = (directionCF * CFrame.fromOrientation(0, 0, RNG:NextNumber(0, TAU)) * CFrame.fromOrientation(math.rad(RNG:NextNumber(MIN_BULLET_SPREAD_ANGLE, MAX_BULLET_SPREAD_ANGLE)), 0, 0)).LookVector
You seem to be misspelling quite a few variables, ensure that you have spelt everything correctly and consistently. (ex: 'startPos' vs 'starPos', 'lookVector' vs 'LookVector', 'directionCF' vs 'direcitonCF', etc).
local tool = script.Parent local handle = tool.Handle local players = game:GetService("Players") local MouseEvent = tool:WaitForChild("MouseEvent") local Mouse = Players.LocalPlayer:GetMouse() local ExpectingInput = false local IsMouseDown = false function OnEquipped(playerMouse) Mouse = playerMouse ExpectingInput = true IsMouseDown = false end function OnUnequipped() ExpectingInput = false IsMouseDown = false end function OnActivated() IsMouseDown = true end function OnDeactivated() IsMouseDown = false end tool.Equipped:Connect(OnEquipped) tool.Unequipped:Connect(OnUnequipped) tool.Activated:Connect(OnActivated) tool.Deactivated:Connect(OnDeactivated) game:GetService("RunService").Stepped:Connect(function() if IsMouseDown == true then MouseEvent:FireServer(Mouse.Hit.Position, handle.Front.Postion end end)
For some reason my gun is unable to shoot bullets. this is the output for whenever i click 21:56:29.060 Gun Testing Place.rbxl auto-recovery file was created - Studio 21:56:34.987 Infinite yield possible on 'Players.dietsoda13.Backpack.Pistol:WaitForChild("MouseEvent")' - Studio 21:56:34.987 Stack Begin - Studio 21:56:34.987 Script 'Players.dietsoda13.Backpack.Pistol.Script', Line 3 - Studio - Script:3 21:56:34.988 Stack End - Studio 21:56:35.039 Infinite yield possible on 'Players.dietsoda13.Backpack.Pistol:WaitForChild("MouseEvent")' - Studio 21:56:35.039 Stack Begin - Studio 21:56:35.039 Script 'Players.dietsoda13.Backpack.Pistol.LocalScript', Line 4 - Studio - LocalScript:4 21:56:35.039 Stack End - Studio
Local script in gun local tool = script.Parent local handle = tool.Handle local Players = game:GetService("Players") local MouseEvent = tool:WaitForChild("MouseEvent") local Mouse = Players.LocalPlayer:GetMouse() local ExpectingInput = false local IsMouseDown = false function OnEquipped(playerMouse) Mouse = playerMouse ExpectingInput = true IsMouseDown = false end function OnUnequipped() ExpectingInput = false IsMouseDown = false end function OnActivated() IsMouseDown = true end function OnDeactivated() IsMouseDown = false end tool.Equipped:Connect(OnEquipped) tool.Unequipped:Connect(OnUnequipped) tool.Activated:Connect(OnActivated) tool.Deactivated:Connect(OnDeactivated) game:GetService("RunService").Stepped:Connect(function() if IsMouseDown == true then MouseEvent:FireServer(Mouse.Hit.Position, handle.Front.Position) end end)
The other script in the gun local tool = script.Parent local handle = tool.Handle local MouseEvent = tool:WaitForChild("MouseEvent") local CanFire = true local MIN_BULLET_SPREAD_ANGLE = 0 local MAX_BULLET_SPREAD_ANGLE = 0 local FIRE_DELAY = 0.25 local BULLETS_PER_SHOT = 1 local RNG = Random.new() local TAU = math.pi * 2 local VisualizeBulletsEvent = game:GetService("ReplicatedStorage"):WaitForChild("VisualizeBulletsEvent") function Fire(endpoint, startpos) if tool.parent:IsA("Backpack") then return end
local direction = (endpoint - handle.Front.Position).Unit local directionCF = CFrame.new(Vector3.new(), direction) local newDirection = (directionCF * CFrame.fromOrientation(0, 0, RNG:NextNumber(0, TAU)) * CFrame.fromOrientation(math.rad(RNG:NextNumber(MIN_BULLET_SPREAD_ANGLE, MAX_BULLET_SPREAD_ANGLE)), 0, 0)).lookVector
starter player script local VisualizeBulletsEvent = game:GetService("ReplicatedStorage"):WaitForChild("VisualizeBulletsEvent") local TweenService = game:GetService("TweenService") local ShotPart = game:GetService("ReplicatedStorage"):WaitForChild("ShotPart") local RunService = game:GetService("RunService") local DebrisService = game:GetService("Debris") local function visualize(startPos, endPos, distance, color)
welp i fiddled with it, i misnamed the MouseEvent as Mouse event. now im getting this: 09:47:25.936 Front is not a valid member of Part "Workspace.dietsoda13.Pistol.Handle" - Client - LocalScript:36 09:47:25.936 Stack Begin - Studio 09:47:25.936 Script 'Players.dietsoda13.Backpack.Pistol.LocalScript', Line 36 - Studio - LocalScript:36 09:47:25.937 Stack End - Studio
my output says "unable to cast instance to vector3" its a line 30 its the regular script in the assault rifle i dont think it has anything to do with the modle i made since i used the bullet you used and arrange all the parents and children like yours heres all my code(its all the same as yours i think) local Tool = script.Parent local Handle = Tool.Handle local MouseEvent = Tool:WaitForChild("MouseEvent") local CanFire = true local MIN_BULLET_SPREAD_ANGLE = 0 local MAX_BULLET_SPREAD_ANGLE = 0 local FIRE_DELAY = 0.2 local BULLETS_PER_SHOT = 1 local RNG = Random.new() local TAU = math.pi * 2 local VisualizeBulletsEvent = game:GetService("ReplicatedStorage"):WaitForChild("VisualizeBulletsEvent") function Fire(endPoint, startPos) if Tool.Parent:IsA("Backpack") then return end local direction = (endPoint - Handle.Front.Position).Unit local directionalCF = CFrame.new(Vector3.new(), direction) local newDirection = (directionalCF * CFrame.fromOrientation(0, 0, RNG:NextNumber(0, TAU)) * CFrame.fromOrientation(math.rad(RNG:NextNumber(MIN_BULLET_SPREAD_ANGLE, MAX_BULLET_SPREAD_ANGLE)), 0, 0)).LookVector local raycastParams = RaycastParams.new() raycastParams.FilterDescendantsInstances = {script.Parent.Parent} raycastParams.FilterType = Enum.RaycastFilterType.Exclude raycastParams.IgnoreWater = true local raycastResult = workspace:Raycast(startPos, newDirection * 250, raycastParams) if raycastResult then if raycastResult.Distance 0.03 then task.wait(FIRE_DELAY) end CanFire = true end)
local tool = script.Parent local handle = tool.Handle local MouseEvent = tool:WaitForChild("MouseEvent") local CanFire = true local MIN_BULLET_SPREAD_ANGLE = 0 local MAX_BULLET_SPREAD_ANGLE = 0 local FIRE_DELAY = 0.25 local BULLETS_PER_SHOT = 1 local RNG = Random.new() local TAU = math.pi * 2 local VisualizeBulletsEvent = game:GetService("ReplicatedStorage"):WaitForChild("VisualizeBulletsEvent") function Fire(endpoint, startPos)
if tool.Parent:isA("Backpack") then return end
local diretion = (endpoint - handle.Front.Position)Unit local direction = CFrame.new(Vector3.new(), direction) local newDirection = (directionCF * CFrame.FromOrientation(0, 0, RNG:NextNumber(0,TAU)) * CFrame.FromOrientation(math.Rad(RNG:NextNumber(MIN_BULLET_SPREAD_ANGLE, MAX_BULLET_SPREAD_ANGLE)), 0, 0)).LookVector
NOT WORKING PLS HELP ASSAULT RIFFLE SCRIPT local Tool = script.Parent local Handle = Tool.Handle local MouseEvent = Tool.MouseEvent local CanFire = true local MIN_BULLET_SPREAD_ANGLE = 0 local MAX_BULLET_SPREAD_ANGLE = 0 local FIRE_DELAY = 0.1 local BULLETS_PER_SHOT = 1 local RNG = Random.new() local TAU = math.pi * 2 local VisualizeBulletsEvent = game:GetService("ReplicatedStorage"):WaitForChild("VisualizeBulletsEvent") function Fire(endPoint, startPos) if Tool.Parent:IsA("Backpack") then return end local direction = (endPoint - Handle.Front.Position).Unit local directionalCF = CFrame.new(Vector3.new(), direction) local newDirection = (directionalCF * CFrame.fromOrientation(0, 0, RNG:NextNumber(0, TAU)) * CFrame.fromOrientation(math.rad(RNG:NextNumber(MIN_BULLET_SPREAD_ANGLE, MAX_BULLET_SPREAD_ANGLE)), 0, 0)).LookVector local raycastParams = RaycastParams.new() raycastParams.FilterDescendantsInstances = {script.Parent.Parent} raycastParams.FilterType = Enum.RaycastFilterType.Exclude raycastParams.IgnoreWater = true local raycastResult = workspace:Raycast(startPos, newDirection * 250, raycastParams) if raycastResult then if raycastResult.Distance 0.03 then task.wait(FIRE_DELAY) end CanFire = false end) LOCAL SCRIPT local tool = script.Parent local handle = tool.handle local players = game:GetService('Players') local MouseEvent = tool:WaitForChild("MouseEvent") local Mouse = Players.LocalPlayers:GetMouse() local ExpectingInput = false local IsMouseDown = false function OnEquipped(playermouse) Mouse = playermouse ExpectingInput = true IsMouseDown = false end function OnUnequipped() ExpectingInput = false IsMouseDown = false end function OnActivated() IsMouseDown = true end function OnDeactivated() IsMouseDown = false end tool.Equipped:Connect(OnEquipped) tool.Unequipped:Connect(OnUnequipped) tool.Activated:Connect(OnActivated) tool.Deactivated:Connect(OnDeactivated) game:GetService("RunService").Stepped:Connect(function() if IsMouseDown == true then MouseEvent:FireServer(Mouse.Hit.Position, handle.front.Position) end end) STARTER PLAYER PACK LOCAL SCRIPT local VisualizeBulletsEvent = game:GetService("ReplicatedStorage"):WaitForChild("VisualizeBulletsEvent") local TweenService = game:GetService("TweenService") local ShotPart = game:GetService("ReplicatedStorage"):WaitForChild("ShotPart") local RunService = game:GetService("RunService") local DebrisService = game:GetService("Debris") local function visualize(startPos, endPos, distance, colour)
Server code:
local Tool = script.Parent
local Handle = Tool.Handle
local MouseEvent = Tool.MouseEvent
local CanFire = true
local MIN_BULLET_SPREAD_ANGLE = 0
local MAX_BULLET_SPREAD_ANGLE = 0
local FIRE_DELAY = 0.1
local BULLETS_PER_SHOT = 1
local RNG = Random.new()
local TAU = math.pi * 2
local VisualizeBulletsEvent = game:GetService("ReplicatedStorage"):WaitForChild("VisualizeBulletsEvent")
function Fire(endPoint, startPos)
if Tool.Parent:IsA("Backpack") then return end
local direction = (endPoint - Handle.Front.Position).Unit
local directionalCF = CFrame.new(Vector3.new(), direction)
local newDirection = (directionalCF * CFrame.fromOrientation(0, 0, RNG:NextNumber(0, TAU)) * CFrame.fromOrientation(math.rad(RNG:NextNumber(MIN_BULLET_SPREAD_ANGLE, MAX_BULLET_SPREAD_ANGLE)), 0, 0)).LookVector
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {script.Parent.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.IgnoreWater = true
local raycastResult = workspace:Raycast(startPos, newDirection * 250, raycastParams)
if raycastResult then
if raycastResult.Distance 0.03 then task.wait(FIRE_DELAY) end
CanFire = true
end)
what about the script??
can I give him a copy of the script so I don't have to write anymore?
BRO I WROTE EVERY SINGLE LINE OF CODE BY PAUSING AND STUFF AND NOW I SEE THIS THE #######
local VisualizeBulletsEvent = game:GetService("ReplicatedStorage"):WaitForChild("VisualizeBulletsEvent")
local Tween Service = game:GetService("TweenService")
local ShotPart = game:GetService("ReplicatedStorage"):WaitForChild("ShotPart")
local RunService = game:GetService("RunService")
local DebrisService = game:GetService("Debris")
local function visualize(startPos, endpos, distance, color)
local bullet = ShotPart:Clone()
bullet:WaitForChild("Middle"):WaitForChild("Trails"):WaitForChild("Inner").Color = ColorSequence.new(color)
bullet:WaitForChild("Middle"):WaitForChild("Trails"):WaitForChild("Outter").Color = ColorSequence.new(color)
bullet:WaitForChild("Middle"):WaitForChild("Light").Color = Color3.new(color)
bullet.Position = startPos
local goal = {}
goal.Position = endPos
local tweenInfo = TweenInfo.new(distance/500)
local tween =TweenService:Create(bullet, tweenInfo, goal)
DebrisService:AddItem(bullet, distance/500)
bullet.Parent = game.Workspace
RunService.RenderStepped:Wait()
RunService.RenderStepped:Wait()
tween:Play()
end
VisualizeBulletsEvent.OnClientEvent:Connect(visualize)
Any problems with this code?
contgrats on hitting 1k i found you a couple days ago btw
Congratulations on 1k!
Thank you so much 😀
i am so happy to see this video becasue i was just about to code a gun!
YO IT WORKS TYSM And how can I add sound effects when the gun shoots?
Here's the code For everyone just trying to help :]
LocalScript in The gun :
local tool = script.Parent
local handle = tool.Handle
local Players = game:GetService("Players")
local MouseEvent = tool:WaitForChild("MouseEvent")
local Mouse = Players.LocalPlayer:GetMouse()
local ExpectingInput = false
local IsMouseDown = false
function OnEquipped(playerMouse)
Mouse = playerMouse
ExpectingInput = true
IsMouseDown = false
end
function OnUnequipped()
ExpectingInput = false
IsMouseDown = false
end
function Activated()
IsMouseDown = true
end
function OnDeactivated()
IsMouseDown = false
end
tool.Equipped:Connect(OnEquipped)
tool.Unequipped:Connect(OnUnequipped)
tool.Activated:Connect(Activated)
tool.Deactivated:Connect(OnDeactivated)
game:GetService("RunService").Stepped:Connect(function()
if IsMouseDown == true then
MouseEvent:FireServer(Mouse.Hit.Position, handle.Front.Position)
end
end)
LocalScript in the starterplayerscript :
local VisualizeBulletsEvent = game:GetService("ReplicatedStorage"):WaitForChild("VisualizeBulletsEvent")
local TweenService = game:GetService("TweenService")
local ShotPart = game:GetService("ReplicatedStorage"):WaitForChild("ShotPart")
local RunService = game:GetService("RunService")
local DebrisService = game:GetService("Debris")
local function visualize(startPos, endPos, distance, color)
local bullet = ShotPart:Clone()
bullet:WaitForChild("Middle"):WaitForChild("Trails"):WaitForChild("Inner").Color = ColorSequence.new(color)
bullet:WaitForChild("Middle"):WaitForChild("Trails"):WaitForChild("Outter").Color = ColorSequence.new(color)
bullet:WaitForChild("Middle"):WaitForChild("Light").Color = Color3.new(color)
bullet.Position = startPos
local goal = {}
goal.Position = endPos
local tweenInfo = TweenInfo.new(distance/500)
local tween = TweenService:Create(bullet, tweenInfo, goal)
DebrisService:AddItem(bullet, distance/500)
bullet.Parent = game.Workspace
RunService.RenderStepped:Wait()
RunService.RenderStepped:Wait()
tween:Play()
end
VisualizeBulletsEvent.OnClientEvent:Connect(visualize)
TYSM MANNN
ur the best man thanks SO MUCH
tysm to add a sound simply [add a sound to the gun] and [Get its Directory] then [Add it to the script]
local sound = tool.Sound ,Sound)
Play:Sound()
Hello, I made the gun successfully the first time (making a zombie game, I made some modifications but other than that everything is working perfectly), but I tried making a pistol now using the same code and parts, and it won't work! I have made sure to make the proper modifications to the scripts to reference the new parts inside the pistol, and I am returning 0 errors in the output. So then i tried just simply duplicating the Rifle I made, and it didn't work! exact same parts and code, no modifications, and it didn't work!!! It seems the way the code is done you can only have one of these guns??? I have tried to make new remote events and a new bullet script to listen for the new remote event to see if this was the problem, and nothing. Another thing I tried was putting "print(print something here)" after lines to make sure they ran, and it when I put a print after my local script fires the event, it prints showing that the code worked, but then the server script never actually hears the event and never runs at all!!! I am at a lost, I will continue to try to fix this and will reply to my own comment if I figure it out myself before a response. Please try to get back to me because this game is looking really good currently and I don't want this to waste too much of my limited time. Thank you so much for the video, and thank you for any potential response!
Interesting. Can you verify that there is a part called 'Front' inside your pistol and that it is positioned where the bullets should come out of and that it is welded to the rest of the gun?
@@Scrip_tix Yeah, there is a part called front, but I've made a new discovery, if I duplicate the assault rifle that is already working, and put it in the starter pack, it works, but if I don't modify a single thing, but instead put the gun in replicated storage, and have a script duplicate the gun and give you the gun, then it doesn't work.
So my idea is, that I am trying to make a pistol as your starting weapon, I got rid of the pistol bc it wasn't working and I am trying to test potential problems using a duplicate of the rifle that I made using this video that actually works. When I put the working rifle in replicated storage, and then once you click play on a gui, duplicate the gun twice and put one in starter gear and one in the players backpack, it doesn't work. It gives you the gun, you can hold it but it wont shoot and won't deal damage. The duplicate of the gun works perfectly fine if I place it in the starter pack though, which might be something to test out, I will update soon if I can find a compromise.
Hey! I figured out the issue! Thank you so much for taking your time to respond to me for absolutely no personal gain, even though I solved it myself, it shows you care a lot about your viewers. I noticed that when I play tested in the current client, that I couldn't see the gun and then remembered that I duplicated the gun and put the gun in starter gear and the starter pack through a local script. Now I put a remote event in and used a regular script, and now it works perfectly!! Thank you for responding and if anyone else somehow has a similar problem, yk what to tell them.
You're welcome!
For some reason when I equip the gun my character flips sideways onto the ground to hold the tool's grip. Would you happen to know how to fix this?
is your tool anchored? if yes,then unanchor it,it's mostly caused by the enforcement to the gravity that the game suggests,meaning if you set your tool anchored,the rotation and position of the tool will be kept,leading you to have this problem
The gun works but I can’t see the bullets, there’s an error in Local Script in Starterplayerscripts about line 10 that says “ColorSequence.new(): table expected.” What does it mean?
Can you show me the code in the localscript?
@@Scrip_tix local VisualizeBulletsEvent = game:GetService("ReplicatedStorage"):WaitForChild("VisualizeBulletsEvent")
local TweenService = game:GetService("TweenService")
local ShotPart = game:GetService("ReplicatedStorage"):WaitForChild("ShotPart")
local RunService = game:GetService("RunService")
local DebrisService = game:GetService("Debris")
local function visualize(startPos, endPos, distance, color)
local bullet = ShotPart:Clone()
bullet:WaitForChild("Middle"):WaitForChild("Trails"):WaitForChild("Inner").Color = ColorSequence.new(color)
bullet:WaitForChild("Middle"):WaitForChild("Trails"):WaitForChild("Outter").Color = ColorSequence.new(color)
bullet:WaitForChild("Middle"):WaitForChild("Light").Color = Color3.new(color)
bullet.Position = startPos
local goal = {}
goal.Position = endPos
local tweenInfo = TweenInfo.new(distance/500)
local tween =TweenService:Create(bullet, tweenInfo, goal)
DebrisService:AddItem(bullet, distance/500)
bullet.Parent = game.Workspace
RunService.RenderStepped:Wait()
RunService.RenderStepped:Wait()
tween:Play()
end
VisualizeBulletsEvent.OnClientEvent:Connect(visualize)
btw now there is another error in the local script of my gun "Handle us not a valid member of Tool "Players.(my absolutely embarrassing user here).Backpack.Pistol" so heres the script for that
local Tool = script.Parent
local Handle = Tool.Handle
local Players = game:GetService("Players")
local MouseEvent = Tool:WaitForChild("MouseEvent")
local Mouse = Players.LocalPlayer:GetMouse()
local ExpectingInput = false
local IsMouseDown = false
function OnEquipped(playerMouse)
Mouse = playerMouse
ExpectingInput = true
IsMouseDown = false
end
function OnUnequipped()
ExpectingInput = false
IsMouseDown = false
end
function OnActivated()
IsMouseDown = true
end
function OnDeactivated()
IsMouseDown = false
end
Tool.Equipped:Connect(OnEquipped)
Tool.Unequipped:Connect(OnUnequipped)
Tool.Activated:Connect(OnActivated)
Tool.Deactivated:Connect(OnDeactivated)
game:GetService("RunService").Stepped:Connect(function()
if IsMouseDown == true then
MouseEvent:FireServer(Mouse.Hit.Position, Handle.Front.Position)
end
end)
@@Scrip_tix the problem isnt showing up anymore for some reason but here's the code
local VisualizeBulletsEvent = game:GetService("ReplicatedStorage"):WaitForChild("VisualizeBulletsEvent")
local TweenService = game:GetService("TweenService")
local ShotPart = game:GetService("ReplicatedStorage"):WaitForChild("ShotPart")
local RunService = game:GetService("RunService")
local DebrisService = game:GetService("Debris")
local function visualize(startPos, endPos, distance, color)
local bullet = ShotPart:Clone()
bullet:WaitForChild("Middle"):WaitForChild("Trails"):WaitForChild("Inner").Color = ColorSequence.new(color)
bullet:WaitForChild("Middle"):WaitForChild("Trails"):WaitForChild("Outter").Color = ColorSequence.new(color)
bullet:WaitForChild("Middle"):WaitForChild("Light").Color = Color3.new(color)
bullet.Position = startPos
local goal = {}
goal.Position = endPos
local tweenInfo = TweenInfo.new(distance/500)
local tween =TweenService:Create(bullet, tweenInfo, goal)
DebrisService:AddItem(bullet, distance/500)
bullet.Parent = game.Workspace
RunService.RenderStepped:Wait()
RunService.RenderStepped:Wait()
tween:Play()
end
VisualizeBulletsEvent.OnClientEvent:Connect(visualize)
now, a new problem is showing up, "Handle is not a valid member of Tool "Players.(myuser).Backpack.Pistol" - Client - LocalScript:2 (in the gun) so heres the other local script
local Tool = script.Parent
local Handle = Tool.Handle
local Players = game:GetService("Players")
local MouseEvent = Tool:WaitForChild("MouseEvent")
local Mouse = Players.LocalPlayer:GetMouse()
local ExpectingInput = false
local IsMouseDown = false
function OnEquipped(playerMouse)
Mouse = playerMouse
ExpectingInput = true
IsMouseDown = false
end
function OnUnequipped()
ExpectingInput = false
IsMouseDown = false
end
function OnActivated()
IsMouseDown = true
end
function OnDeactivated()
IsMouseDown = false
end
Tool.Equipped:Connect(OnEquipped)
Tool.Unequipped:Connect(OnUnequipped)
Tool.Activated:Connect(OnActivated)
Tool.Deactivated:Connect(OnDeactivated)
game:GetService("RunService").Stepped:Connect(function()
if IsMouseDown == true then
MouseEvent:FireServer(Mouse.Hit.Position, Handle.Front.Position)
end
end)
@@Scrip_tix The problem changed now, it's "Handle is not a valid member of Tool "Player.DxrkShadowVibez.Backpack.Pistol" so heres the local script for that says its line 2 btw
local Tool = script.Parent
local Handle = Tool.Handle
local Players = game:GetService("Players")
local MouseEvent = Tool:WaitForChild("MouseEvent")
local Mouse = Players.LocalPlayer:GetMouse()
local ExpectingInput = false
local IsMouseDown = false
function OnEquipped(playerMouse)
Mouse = playerMouse
ExpectingInput = true
IsMouseDown = false
end
function OnUnequipped()
ExpectingInput = false
IsMouseDown = false
end
function OnActivated()
IsMouseDown = true
end
function OnDeactivated()
IsMouseDown = false
end
Tool.Equipped:Connect(OnEquipped)
Tool.Unequipped:Connect(OnUnequipped)
Tool.Activated:Connect(OnActivated)
Tool.Deactivated:Connect(OnDeactivated)
game:GetService("RunService").Stepped:Connect(function()
if IsMouseDown == true then
MouseEvent:FireServer(Mouse.Hit.Position, Handle.Front.Position)
end
end)
@@Scrip_tix the problem changed to "Handle is not a valid member of Tool Players.DxrkShaadowVibez.Backpack.Pistol" line 2 so heres the local script for the gun
local Tool = script.Parent
local Handle = Tool.Handle
local Players = game:GetService("Players")
local MouseEvent = Tool:WaitForChild("MouseEvent")
local Mouse = Players.LocalPlayer:GetMouse()
local ExpectingInput = false
local IsMouseDown = false
function OnEquipped(playerMouse)
Mouse = playerMouse
ExpectingInput = true
IsMouseDown = false
end
function OnUnequipped()
ExpectingInput = false
IsMouseDown = false
end
function OnActivated()
IsMouseDown = true
end
function OnDeactivated()
IsMouseDown = false
end
Tool.Equipped:Connect(OnEquipped)
Tool.Unequipped:Connect(OnUnequipped)
Tool.Activated:Connect(OnActivated)
Tool.Deactivated:Connect(OnDeactivated)
game:GetService("RunService").Stepped:Connect(function()
if IsMouseDown == true then
MouseEvent:FireServer(Mouse.Hit.Position, Handle.Front.Position)
end
end)
do you *have* to, put the gun on the StarterPack? i want it to make it, so you have to grab it, then you can use it.
when i put the gun i made in my starter pack it didnt show up in my inventory? does it only work with your model
i checked output and it says
12:12:42.940 Front is not a valid member of Part "Workspace.Baste513.Pistol.Handle" - Client - Pistol Gun Local Script:36
If i go to close to the enemy i wanna shoot, i cannot really (or cant literally) see the bullets, because of their size anymore
Hey, Script works perfectly fine but I was wondering if you could help me add an integrated system inside the preexisting scripts to play a bullet sound every time a bullet comes out of the gun. I can a write a separate script outside to match the weapons fire delay but once I start adding more guns I think I will run into some issues with timing. Can you help me write something that uses the fire delay variable?
This error looks like it shouldn't be happening i have copied it word for word and i dont know what to do, it is in line 18 for Local Script: local tween =TweenService:Create(bullet, tweenInfo, goal)
Error Message
01:27:49.996 Players.Drtrayblox123yea.PlayerScripts.LocalScript:18: attempt to index nil with 'Create' - Studio
Can you show me your code for the localscript?
@@Scrip_tix local VisualizeBulletsEvent = game:GetService("ReplicatedStorage"):WaitForChild("VisualizeBulletsEvent")
local Tween Service = game:GetService("TweenService")
local ShotPart = game:GetService("ReplicatedStorage"):WaitForChild("ShotPart")
local RunService = game:GetService("RunService")
local DebrisService = game:GetService("Debris")
local function visualize(startPos, endpos, distance, color)
local bullet = ShotPart:Clone()
bullet:WaitForChild("Middle"):WaitForChild("Trails"):WaitForChild("Inner").Color = ColorSequence.new(color)
bullet:WaitForChild("Middle"):WaitForChild("Trails"):WaitForChild("Outter").Color = ColorSequence.new(color)
bullet:WaitForChild("Middle"):WaitForChild("Light").Color = Color3.new(color)
bullet.Position = startPos
local goal = {}
goal.Position = endPos
local tweenInfo = TweenInfo.new(distance/500)
local tween =TweenService:Create(bullet, tweenInfo, goal)
DebrisService:AddItem(bullet, distance/500)
bullet.Parent = game.Workspace
RunService.RenderStepped:Wait()
RunService.RenderStepped:Wait()
tween:Play()
end
VisualizeBulletsEvent.OnClientEvent:Connect(visualize)
@@Scrip_tix local VisualizeBulletsEvent = game:GetService("ReplicatedStorage"):WaitForChild("VisualizeBulletsEvent")
local Tween Service = game:GetService("TweenService")
local ShotPart = game:GetService("ReplicatedStorage"):WaitForChild("ShotPart")
local RunService = game:GetService("RunService")
local DebrisService = game:GetService("Debris")
local function visualize(startPos, endpos, distance, color)
local bullet = ShotPart:Clone()
bullet:WaitForChild("Middle"):WaitForChild("Trails"):WaitForChild("Inner").Color = ColorSequence.new(color)
bullet:WaitForChild("Middle"):WaitForChild("Trails"):WaitForChild("Outter").Color = ColorSequence.new(color)
bullet:WaitForChild("Middle"):WaitForChild("Light").Color = Color3.new(color)
bullet.Position = startPos
local goal = {}
goal.Position = endPos
local tweenInfo = TweenInfo.new(distance/500)
local tween =TweenService:Create(bullet, tweenInfo, goal)
DebrisService:AddItem(bullet, distance/500)
bullet.Parent = game.Workspace
RunService.RenderStepped:Wait()
RunService.RenderStepped:Wait()
tween:Play()
end
VisualizeBulletsEvent.OnClientEvent:Connect(visualize)
@@Scrip_tix local VisualizeBulletsEvent = game:GetService("ReplicatedStorage"):WaitForChild("VisualizeBulletsEvent")
local Tween Service = game:GetService("TweenService")
local ShotPart = game:GetService("ReplicatedStorage"):WaitForChild("ShotPart")
local RunService = game:GetService("RunService")
local DebrisService = game:GetService("Debris")
local function visualize(startPos, endpos, distance, color)
local bullet = ShotPart:Clone()
bullet:WaitForChild("Middle"):WaitForChild("Trails"):WaitForChild("Inner").Color = ColorSequence.new(color)
bullet:WaitForChild("Middle"):WaitForChild("Trails"):WaitForChild("Outter").Color = ColorSequence.new(color)
bullet:WaitForChild("Middle"):WaitForChild("Light").Color = Color3.new(color)
bullet.Position = startPos
local goal = {}
goal.Position = endPos
local tweenInfo = TweenInfo.new(distance/500)
local tween =TweenService:Create(bullet, tweenInfo, goal)
DebrisService:AddItem(bullet, distance/500)
bullet.Parent = game.Workspace
RunService.RenderStepped:Wait()
RunService.RenderStepped:Wait()
tween:Play()
end
VisualizeBulletsEvent.OnClientEvent:Connect(visualize)
@@Scrip_tix
Line 18 Error: Code
local tween =TweenService:Create(bullet, tweenInfo, goal)
Question:Does this work for 2+ player servers(when tool.activated doesn't activate other tools from other clients)?
error:Infinite yield possible on 'ShotPart.Middle.Light:WaitForChild("Inner")
Show me your code :3
@@Scrip_tix you can't use :3 only me :3
@@Aika33700 mind being quiet?
@@Architect532 no
Ok i have the pistol working, please ignore my other comments. How would i change the script to make it only fire once with a small cooldown per mouseclick?
bro how do i fix when i click it fire but if i click once again its still fire can u pls fix
i don't understand, it doesn't show up in my inventory
i try starter pack, i try starter player but it simply doesn't show up
If anyone can help just response
Hey its not working! I need help.
local Tool = script.Parent
local Handle = Tool.Handle
local MouseEvent = Tool.MouseEvent
local CanFire = true
local MIN_BULLET_SPREAD_ANGLE = 0
local MAX_BULLET_SPREAD_ANGLE = 0
local FIRE_DELAY = 0.1
local BULLETS_PER_SHOT = 1
local RNG = Random.new()
local TAU = math.pi * 2
local VisualizeBulletsEvent = game:GetService("ReplicatedStorage"):WaitForChild("VisualizeBulletsEvent")
function Fire(endPoint, startPos)
if Tool.Parent:IsA("Backpack") then return end
local direction = (endPoint - Handle.Front.Position).Unit
local directionalCF = CFrame.new(Vector3.new(), direction)
local newDirection = (directionalCF * CFrame.fromOrientation(0, 0, RNG:NextNumber(0, TAU)) * CFrame.fromOrientation(math.rad(RNG:NextNumber(MIN_BULLET_SPREAD_ANGLE, MAX_BULLET_SPREAD_ANGLE)), 0, 0)).LookVector
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {script.Parent.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.IgnoreWater = true
local raycastResult = workspace:Raycast(startPos, newDirection * 250, raycastParams)
if raycastResult then
if raycastResult.Distance 0.03 then task.wait(FIRE_DELAY) end
CanFire = true
end)
i didnt know what to do at the end lol
Any errors in the output?
@@Scrip_tix no idk
how to make it shoot with one click and not by pressing the left mouse button?
Is it server sided? Do the shots appear on others screens?
The raycasting is done on the server side, with a RemoteEvent that fires to tell clients to create the bullet effects, so yes, the shots will appear on everyone else's screens :3
Yeah i noticed, I added reloading and SFX for the gun. Thanks for making the hard part lol@@Scrip_tix
My Python brain tried putting a colon so many times after any if/else statement.
lmao
Yo how to make shoot sounds when the gun shoots?
I been trying for the past 3 hours to make the raycast be destroyed after hitting a part, pls help
Can you show me your code? Are there any errors?
@@Scrip_tix Its not my code, its yours.In the video the shots also go through everything. Im not complaining it works fine, but i was wondering IF you could make a tutorial on how to make the bullet disappear after it touches a part. All my code is the same as your.
this too hard i gave up
Womp womp, who said coding was easy?
Me also
weakling.
Yeah me either
@@yenj36 lol
btw I did it on First Person but then it doenst shoot anymore
what do I do???
Interesting, it still works for me after doing in first person. Can you show me your code?
@@Scrip_tix Assault Rifle Script
local tool = script.Parent
local handle = tool.Handle
local MouseEvent = tool:WaitForChild("MouseEvent")
local CanFire = true
local MIN_BULLET_SPREAD_ANGLE = 0
local MAX_BULLET_SPREAD_ANGLE = 0
local FIRE_DELAY = 0.25
local BULLETS_PER_SHOT = 1
local RNG = Random.new()
local TAU = math.pi * 2
local VisualizeBulletsEvent = game:GetService("ReplicatedStorage"):WaitForChild("VisualizeBulletsEvent")
function Fire(endpoint, starPos)
if tool.Parent:IsA("BackPack") then return end
local direction = (endpoint - handle.Front.Position).Unit
local direcitonCF = CFrame.new(Vector3.new(), direction)
local newDirection = (directionCF * CFrame.fromOrientation(0, 0, RNG:NextNumber(0, TAU)) * CFrame.fromOrientation(math.rad(RNG:NextNumber(MIN_BULLET_SPREAD_ANGLE, MAX_BULLET_SPREAD_ANGLE)), 0, 0)).LookVector
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {script.Parent.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.IgnoreWater = true
local raycastResult = workspace:Raycast(startPos, newDirection * 250, raycastParams)
if raycastResult then
if raycastResult.Distance 0.03 then task.wait(FIRE_DELAY) end
CanFire = true
end)
You seem to be misspelling quite a few variables, ensure that you have spelt everything correctly and consistently. (ex: 'startPos' vs 'starPos', 'lookVector' vs 'LookVector', 'directionCF' vs 'direcitonCF', etc).
How long have you been scripting
if only there was something could copy and paste the copy then i wouldn't take so long
dose it also shoot pepole if it dose can u pls make it not shoot pepole only like npc??
When you're checking if they have a humanoid, you can also do:
if not game:Players:FindFirstChild(hitPart.Parent.Name) then
@@Scrip_tix ok thx
cool
bro i did the whole thing correct and it didnt work
local tool = script.Parent
local handle = tool.Handle
local players = game:GetService("Players")
local MouseEvent = tool:WaitForChild("MouseEvent")
local Mouse = Players.LocalPlayer:GetMouse()
local ExpectingInput = false
local IsMouseDown = false
function OnEquipped(playerMouse)
Mouse = playerMouse
ExpectingInput = true
IsMouseDown = false
end
function OnUnequipped()
ExpectingInput = false
IsMouseDown = false
end
function OnActivated()
IsMouseDown = true
end
function OnDeactivated()
IsMouseDown = false
end
tool.Equipped:Connect(OnEquipped)
tool.Unequipped:Connect(OnUnequipped)
tool.Activated:Connect(OnActivated)
tool.Deactivated:Connect(OnDeactivated)
game:GetService("RunService").Stepped:Connect(function()
if IsMouseDown == true then
MouseEvent:FireServer(Mouse.Hit.Position, handle.Front.Postion
end
end)
Bro, the raycast dosent work
It should, as I have just demonstrated it. Can I see your code?
Nevermind, i fixed it
How tf did you learn all this shi#?
this was very confusing
it didnt work.
Are there any errors in the output?
Now make a part 2 where you have to click "R" to reload
For some reason my gun is unable to shoot bullets. this is the output for whenever i click
21:56:29.060 Gun Testing Place.rbxl auto-recovery file was created - Studio
21:56:34.987 Infinite yield possible on 'Players.dietsoda13.Backpack.Pistol:WaitForChild("MouseEvent")' - Studio
21:56:34.987 Stack Begin - Studio
21:56:34.987 Script 'Players.dietsoda13.Backpack.Pistol.Script', Line 3 - Studio - Script:3
21:56:34.988 Stack End - Studio
21:56:35.039 Infinite yield possible on 'Players.dietsoda13.Backpack.Pistol:WaitForChild("MouseEvent")' - Studio
21:56:35.039 Stack Begin - Studio
21:56:35.039 Script 'Players.dietsoda13.Backpack.Pistol.LocalScript', Line 4 - Studio - LocalScript:4
21:56:35.039 Stack End - Studio
Local script in gun
local tool = script.Parent
local handle = tool.Handle
local Players = game:GetService("Players")
local MouseEvent = tool:WaitForChild("MouseEvent")
local Mouse = Players.LocalPlayer:GetMouse()
local ExpectingInput = false
local IsMouseDown = false
function OnEquipped(playerMouse)
Mouse = playerMouse
ExpectingInput = true
IsMouseDown = false
end
function OnUnequipped()
ExpectingInput = false
IsMouseDown = false
end
function OnActivated()
IsMouseDown = true
end
function OnDeactivated()
IsMouseDown = false
end
tool.Equipped:Connect(OnEquipped)
tool.Unequipped:Connect(OnUnequipped)
tool.Activated:Connect(OnActivated)
tool.Deactivated:Connect(OnDeactivated)
game:GetService("RunService").Stepped:Connect(function()
if IsMouseDown == true then
MouseEvent:FireServer(Mouse.Hit.Position, handle.Front.Position)
end
end)
The other script in the gun
local tool = script.Parent
local handle = tool.Handle
local MouseEvent = tool:WaitForChild("MouseEvent")
local CanFire = true
local MIN_BULLET_SPREAD_ANGLE = 0
local MAX_BULLET_SPREAD_ANGLE = 0
local FIRE_DELAY = 0.25
local BULLETS_PER_SHOT = 1
local RNG = Random.new()
local TAU = math.pi * 2
local VisualizeBulletsEvent = game:GetService("ReplicatedStorage"):WaitForChild("VisualizeBulletsEvent")
function Fire(endpoint, startpos)
if tool.parent:IsA("Backpack") then return end
local direction = (endpoint - handle.Front.Position).Unit
local directionCF = CFrame.new(Vector3.new(), direction)
local newDirection = (directionCF * CFrame.fromOrientation(0, 0, RNG:NextNumber(0, TAU)) * CFrame.fromOrientation(math.rad(RNG:NextNumber(MIN_BULLET_SPREAD_ANGLE, MAX_BULLET_SPREAD_ANGLE)), 0, 0)).lookVector
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {script.Parent.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.IgnoreWater = true
local raycastResult = workspace:Raycast(startPos, newDirection * 250, raycastParams)
if raycastResult then
if raycastResult.Distance 0.03 then task.wait(FIRE_DELAY) end
CanFire = true
end)
starter player script
local VisualizeBulletsEvent = game:GetService("ReplicatedStorage"):WaitForChild("VisualizeBulletsEvent")
local TweenService = game:GetService("TweenService")
local ShotPart = game:GetService("ReplicatedStorage"):WaitForChild("ShotPart")
local RunService = game:GetService("RunService")
local DebrisService = game:GetService("Debris")
local function visualize(startPos, endPos, distance, color)
local bullet = ShotPart:Clone()
bullet:WaitForChild("Middle"):WaitForChild("Trails"):WaitForChild("Inner").Color = ColorSequence.new(color)
bullet:WaitForChild("Middle"):WaitForChild("Trails"):WaitForChild("Outter").Color = ColorSequence.new(color)
bullet:WaitForChild("Middle"):WaitForChild("Light").Color = Color3.new(color)
bullet.Position = startPos
local goal = {}
goal.Position = endPos
local tweenInfo = TweenInfo.new(distance/500)
local tween =TweenService:Create(bullet, tweenInfo, goal)
DebrisService:AddItem(bullet, distance/500)
bullet.Parent =game.Workspace
RunService.RenderStepped:Wait()
RunService.RenderStepped:Wait()
tween:Play()
end
VisualizeBulletsEvent.OnClientEvent:Connect(visualize)
welp i fiddled with it, i misnamed the MouseEvent as Mouse event. now im getting this:
09:47:25.936 Front is not a valid member of Part "Workspace.dietsoda13.Pistol.Handle" - Client - LocalScript:36
09:47:25.936 Stack Begin - Studio
09:47:25.936 Script 'Players.dietsoda13.Backpack.Pistol.LocalScript', Line 36 - Studio - LocalScript:36
09:47:25.937 Stack End - Studio
ok so it works on the gun you provided, i was trying to use a model i made. clearly that won't work too well
my output says "unable to cast instance to vector3"
its a line 30
its the regular script in the assault rifle
i dont think it has anything to do with the modle i made since i used the bullet you used and arrange all the parents and children like yours
heres all my code(its all the same as yours i think)
local Tool = script.Parent
local Handle = Tool.Handle
local MouseEvent = Tool:WaitForChild("MouseEvent")
local CanFire = true
local MIN_BULLET_SPREAD_ANGLE = 0
local MAX_BULLET_SPREAD_ANGLE = 0
local FIRE_DELAY = 0.2
local BULLETS_PER_SHOT = 1
local RNG = Random.new()
local TAU = math.pi * 2
local VisualizeBulletsEvent = game:GetService("ReplicatedStorage"):WaitForChild("VisualizeBulletsEvent")
function Fire(endPoint, startPos)
if Tool.Parent:IsA("Backpack") then return end
local direction = (endPoint - Handle.Front.Position).Unit
local directionalCF = CFrame.new(Vector3.new(), direction)
local newDirection = (directionalCF * CFrame.fromOrientation(0, 0, RNG:NextNumber(0, TAU)) * CFrame.fromOrientation(math.rad(RNG:NextNumber(MIN_BULLET_SPREAD_ANGLE, MAX_BULLET_SPREAD_ANGLE)), 0, 0)).LookVector
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {script.Parent.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.IgnoreWater = true
local raycastResult = workspace:Raycast(startPos, newDirection * 250, raycastParams)
if raycastResult then
if raycastResult.Distance 0.03 then task.wait(FIRE_DELAY) end
CanFire = true
end)
bro just use smth from the toolbox
local tool = script.Parent
local handle = tool.Handle
local MouseEvent = tool:WaitForChild("MouseEvent")
local CanFire = true
local MIN_BULLET_SPREAD_ANGLE = 0
local MAX_BULLET_SPREAD_ANGLE = 0
local FIRE_DELAY = 0.25
local BULLETS_PER_SHOT = 1
local RNG = Random.new()
local TAU = math.pi * 2
local VisualizeBulletsEvent = game:GetService("ReplicatedStorage"):WaitForChild("VisualizeBulletsEvent")
function Fire(endpoint, startPos)
if tool.Parent:isA("Backpack") then return end
local diretion = (endpoint - handle.Front.Position)Unit
local direction = CFrame.new(Vector3.new(), direction)
local newDirection = (directionCF * CFrame.FromOrientation(0, 0, RNG:NextNumber(0,TAU)) * CFrame.FromOrientation(math.Rad(RNG:NextNumber(MIN_BULLET_SPREAD_ANGLE, MAX_BULLET_SPREAD_ANGLE)), 0, 0)).LookVector
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {script.Parent.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.IgnoreWater = true
local raycastResult = workspace:Raycast(startPos, newDirection * 250, raycastParams)
if raycastResult then
if raycastResult.Distance 0.03 then task.wait(Fire_Delay) end
CanFire = true
end)
NOT WORKING PLS HELP
ASSAULT RIFFLE
SCRIPT
local Tool = script.Parent
local Handle = Tool.Handle
local MouseEvent = Tool.MouseEvent
local CanFire = true
local MIN_BULLET_SPREAD_ANGLE = 0
local MAX_BULLET_SPREAD_ANGLE = 0
local FIRE_DELAY = 0.1
local BULLETS_PER_SHOT = 1
local RNG = Random.new()
local TAU = math.pi * 2
local VisualizeBulletsEvent = game:GetService("ReplicatedStorage"):WaitForChild("VisualizeBulletsEvent")
function Fire(endPoint, startPos)
if Tool.Parent:IsA("Backpack") then return end
local direction = (endPoint - Handle.Front.Position).Unit
local directionalCF = CFrame.new(Vector3.new(), direction)
local newDirection = (directionalCF * CFrame.fromOrientation(0, 0, RNG:NextNumber(0, TAU)) * CFrame.fromOrientation(math.rad(RNG:NextNumber(MIN_BULLET_SPREAD_ANGLE, MAX_BULLET_SPREAD_ANGLE)), 0, 0)).LookVector
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {script.Parent.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.IgnoreWater = true
local raycastResult = workspace:Raycast(startPos, newDirection * 250, raycastParams)
if raycastResult then
if raycastResult.Distance 0.03 then task.wait(FIRE_DELAY) end
CanFire = false
end)
LOCAL SCRIPT
local tool = script.Parent
local handle = tool.handle
local players = game:GetService('Players')
local MouseEvent = tool:WaitForChild("MouseEvent")
local Mouse = Players.LocalPlayers:GetMouse()
local ExpectingInput = false
local IsMouseDown = false
function OnEquipped(playermouse)
Mouse = playermouse
ExpectingInput = true
IsMouseDown = false
end
function OnUnequipped()
ExpectingInput = false
IsMouseDown = false
end
function OnActivated()
IsMouseDown = true
end
function OnDeactivated()
IsMouseDown = false
end
tool.Equipped:Connect(OnEquipped)
tool.Unequipped:Connect(OnUnequipped)
tool.Activated:Connect(OnActivated)
tool.Deactivated:Connect(OnDeactivated)
game:GetService("RunService").Stepped:Connect(function()
if IsMouseDown == true then
MouseEvent:FireServer(Mouse.Hit.Position, handle.front.Position)
end
end)
STARTER PLAYER PACK LOCAL SCRIPT
local VisualizeBulletsEvent = game:GetService("ReplicatedStorage"):WaitForChild("VisualizeBulletsEvent")
local TweenService = game:GetService("TweenService")
local ShotPart = game:GetService("ReplicatedStorage"):WaitForChild("ShotPart")
local RunService = game:GetService("RunService")
local DebrisService = game:GetService("Debris")
local function visualize(startPos, endPos, distance, colour)
local bullet = ShotPart:Clone()
bullet:WaitForChild("Middle"):WaitForChild("Trails"):WaitForChild("Inner").Color = ColorSequence.new(colour)
bullet:WaitForChild("Middle"):WaitForChild("Trails"):WaitForChild("Outter").Color = ColorSequence.new(colour)
bullet:WaitForChild("Middle"):WaitForChild("Light").Color = Color3.new(colour)
bullet.Position = startPos
local goal = {}
goal.Position = endPos
local tweenInfo = TweenInfo.new(distance\500)
local tween =TweenService:Create(bullet, tweenInfo, goal)
DebrisService:AddItem(bullet, distance\500)
bullet.Parent = game.Workspace
RunService.RenderStepped:Wait()
RunService.RenderStepped:Wait()
tween:PLAY()
end
VisualizeBulletsEvent.OnClientEvent:Connect(visualize)
Any errors in the output? Also, in the assault rifle script, you wrote "LookVector" instead of "lookVector"
Thank you so much for replying so fast! I will check if there is any error in the output and tell it to you thank you so much
bro in the output it sayinh that .Backpack.Assault Rifle.LocalScript:6: attempt to index nil with 'LocalPlayers'
now i found that p in players was not in caps at line 3 so i changed it and now its saying LocalPlayers is not a valid member of Players "Players"
It's 'LocalPlayer' not "LocalPlayers"