So this is my second or third tutorial, hope you like it! Point out any bugs or anything you think I should change in the comments, and lets get moving! (a few pictures and kinda long)
ALSO : I just changed incapaz’s gun tutorial to fit a taser, credit to his tutorial
So first we will get a taser model, for the this tutorial I will be using this
Remove the scripts, the camera, welds, etc until we have this
Then rename “Pain” to Shoot. And change its transparency so we can see it
this position the taser mesh so that’s its straight and not curved. Then put the Shoot object right in front of it, this is where we will cast the ray from, so keep that in mind.
Then insert an IntValue and Name it AmmoCount, and change its value to 1
Add a script and name it Backend
Add a local script and name it Frontend
Add a RemoteEvent and name it OnShoot
Now we have this! (Make sure to weld the Handle to the Shoot part)
Now add a RemoteEvent to replicated storage called UpdateAmmo
And make your guns bullet(don’t worry about its length) I will make mine a neon blue part
Now open up the script(Backend)
And lets make some variables.
local tool = script.Parent -- the taser
local shoot_part = tool:WaitForChild("Shoot") -- the shoot part
local remote = tool:WaitForChild("OnShoot") -- the remote event inside the tool
local ammoCount = script.Parent:WaitForChild("AmmoCount") -- ammo count value
local Workspace = game:GetService("Workspace") -- the workspace
local ReplicatedStorage = game:GetService("ReplicatedStorage") -- replicated storage
local updateAmmo = ReplicatedStorage.UpdateAmmo -- replicated storage event
ammoCount.Value = 1 -- another ammo count variable thing
next lets go into the front end and fire the event.
(don’t add anything to this script that isn’t mentioned it breaks if you do for some reason, might just be me idk)
local tool = script.Parent
local remote = tool:WaitForChild("OnShoot")
local ammoCount = script.Parent:WaitForChild("AmmoCount")
local Players = game:GetService("Players")
local client = Players.LocalPlayer
local cursor = client:GetMouse()
tool.Activated:Connect(function()
remote:FireServer(cursor.Hit.Position)
end)
Now go back into backend!
We will connect the event so we can detect when it was fired and do the rest of the scripting in the one script and o where else
remote.OnServerEvent:Connect(function(player, position)
end)
next we will do some stuff for the ray and get a print in there!
remote.OnServerEvent:Connect(function(player, position)
print("Taser fired")
local origin = shoot_part.Position
local direction = (position - origin).Unit*60 -- CHANGE 60 TO THE MAX DISTANCE YOU WANT YOUR TASER TO FIRE
local result = Workspace:Raycast(origin, direction)
end)
Now we will right some code for the ammo and another print!
remote.OnServerEvent:Connect(function(player, position)
print("Taser fired")
local origin = shoot_part.Position
local direction = (position - origin).Unit*60
local result = Workspace:Raycast(origin, direction)
if ammoCount.Value < 1 then -- seeing if they have ammo to reload
ammoCount.Value = 1 -- giving them the ammo
wait(1) -- change this to your reload time
else
ammoCount.Value -= 1 -- taking the ammo if they fire
print("ammo used")
end)
now we will determine the bullet clone and some other math for the ray.
remote.OnServerEvent:Connect(function(player, position)
print("Taser fired")
local origin = shoot_part.Position
local direction = (position - origin).Unit*60
local result = Workspace:Raycast(origin, direction)
if ammoCount.Value < 1 then -- seeing if they have ammo to reload
ammoCount.Value = 1 -- giving them the ammo
wait(1) -- change this to your reload time
else
ammoCount.Value -= 1
print("ammo used")
local intersection = result and result.Position or origin + direction
local distance = (origin - intersection).Magnitude
local bullet_clone = ReplicatedStorage.Bullet:Clone()
bullet_clone.Size = Vector3.new(0.1, 0.1, distance)
bullet_clone.CFrame = CFrame.new(origin, intersection)*CFrame.new(0, 0, -distance/2)
bullet_clone.Parent = Workspace
bullet_clone.Anchored = true
bullet_clone.CanCollide = false
end)
Now you can see we have some stuff going!
Next we will make the tased function so that when they get shot that’s what happens
remote.OnServerEvent:Connect(function(player, position)
print("Taser fired")
local origin = shoot_part.Position
local direction = (position - origin).Unit*60
local result = Workspace:Raycast(origin, direction)
if ammoCount.Value < 1 then -- seeing if they have ammo to reload
ammoCount.Value = 1 -- giving them the ammo
wait(1) -- change this to your reload time
else
ammoCount.Value -= 1
print("ammo used")
local intersection = result and result.Position or origin + direction
local distance = (origin - intersection).Magnitude
local bullet_clone = ReplicatedStorage.Bullet:Clone()
bullet_clone.Size = Vector3.new(0.1, 0.1, distance)
bullet_clone.CFrame = CFrame.new(origin, intersection)*CFrame.new(0, 0, -distance/2)
bullet_clone.Parent = Workspace
bullet_clone.Anchored = true
bullet_clone.CanCollide = false
if result then
print("if result") -- gonna be honest I completely forget what this part was for
local part = result.Instance
local Humanoid = part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid")
local function tased()
-- whatever you want to happen when they get tased put it here
Humanoid.JumpPower = 0 -- this and walkseed makes them unable to move
Humanoid.WalkSpeed = 0
Humanoid.Sit = true -- makes them fall
wait(4) -- change 4 to whatever you want for your wait time
Humanoid.JumpPower = 50 -- changes back to the normal stuff
Humanoid.WalkSpeed = 16
end
end)
Now we detect if it hit a humanoid( or just a player) and then tell it what to do
local tool = script.Parent -- the taser
local shoot_part = tool:WaitForChild("Shoot") -- the shoot part
local remote = tool:WaitForChild("OnShoot") -- the remote event inside the tool
local ammoCount = script.Parent:WaitForChild("AmmoCount") -- ammo count value
local Workspace = game:GetService("Workspace") -- the workspace
local ReplicatedStorage = game:GetService("ReplicatedStorage") -- replicated storage
local updateAmmo = ReplicatedStorage.UpdateAmmo -- replicated storage event
ammoCount.Value = 1 -- another ammo count variable thing
remote.OnServerEvent:Connect(function(player, position)
print("Taser fired")
local origin = shoot_part.Position
local direction = (position - origin).Unit*60
local result = Workspace:Raycast(origin, direction)
if ammoCount.Value < 1 then -- seeing if they have ammo to reload
ammoCount.Value = 1 -- giving them the ammo
wait(1) -- change this to your reload time
else
ammoCount.Value -= 1
print("ammo used")
local intersection = result and result.Position or origin + direction
local distance = (origin - intersection).Magnitude
local bullet_clone = ReplicatedStorage.Bullet:Clone()
bullet_clone.Size = Vector3.new(0.1, 0.1, distance)
bullet_clone.CFrame = CFrame.new(origin, intersection)*CFrame.new(0, 0, -distance/2)
bullet_clone.Parent = Workspace
bullet_clone.Anchored = true
bullet_clone.CanCollide = false
if result then
print("if result") -- gonna be honest I completely forget what this part was for
local part = result.Instance
local Humanoid = part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid")
local function tased()
-- whatever you want to happen when they get tased put it here
Humanoid.JumpPower = 0 -- this and walkseed makes them unable to move
Humanoid.WalkSpeed = 0
Humanoid.Sit = true -- makes them fall
wait(4) -- change 4 to whatever you want for your wait time
Humanoid.JumpPower = 50 -- changes back to the normal stuff
Humanoid.WalkSpeed = 16
end
if Humanoid then
print("humanoid tasered")
tased() -- fires tase function
print("Destroying bullet clone")
wait(1)
bullet_clone:Destroy() -- destroys bullet clone
print("destroyed bullet clone")
else
-- this and the next else or for if it hits air or a part it still destroys the bullet
print("Destroying bullet clone2")
wait(1)
bullet_clone:Destroy()
print("destroyed bullet clone2")
end
else
print("Destroying bullet clone3")
wait(1)
bullet_clone:Destroy()
print("destroyed bullet clone3")
end
end
end)
and that is everything! Sorry for bad spelling or grammar, if I got anything wrong or you have any questions ask them in the comments, hope you like it!
DO NOT FORGET TO WELD THE HANDLE TO THE SHOOT PART
Random bugs I don’t feel like figuring out because in running on 2 hours of sleep:
Sometimes the bullets won’t get destroyed or will stay in the air for extended periods of time.