Starter Character Robot GatlingGun firing bullet not working

My goal is to make the right arm of my robot fire bullets, thing is, it’s not a tool. I have a local script that is parented to my StarterCharacter and here is the script and the robot:


I want it to shoot bullets out of it’s right arm

(Right Gatling Gun)
‘’’
local tool = script.Parent.RightGatlingGun --Getting the Tool
local player = game:GetService(“Players”).LocalPlayer --Getting the player
local mouse = player:GetMouse() --Getting the mouse
local difference = 0 --difference between head and mouse
local replicatedStorage = game:GetService(“ReplicatedStorage”)
local shootevent = replicatedStorage:WaitForChild(“ShootEvent”)

mouse.Button1Down:Connect(function()
local head = game.Workspace[player.Name].Head.CFrame.lookVector – finding players name and where they look
local mouse = CFrame.new(game.Workspace[player.Name].Head.Position,mouse.Hit.p).lookVector
difference = (head-mouse)
local ray = Ray.new(tool.CFrame.p, (player:GetMouse().Hit.p - tool.CFrame.p).unit*300)
local part,position = game.Workspace:FindPartOnRay(ray,player.Character,false,true)
if difference.magnitude < 1.33 then
shootevent:FireServer(tool,position,part)

		end

end)

‘’’
Server Script:
‘’’
local replicatedStorage = game:GetService(“ReplicatedStorage”)
local damage = 32

replicatedStorage.ShootEvent.OnServerEvent:Connect(function(player,tool,position,part)
if game.Workspace[player.Name].Humanoid.Health <= 0 then
else
local distance = (tool.CFrame.p - position).magnitude
if game.Workspace:FindFirstChild(player.Name…“'s Trajectory”) then
game.Workspace:FindFirstChild(player.Name…“'s Trajectory”):Destroy()

	end
	local trajectory = Instance.new("Part",game.Workspace)
	trajectory.BrickColor = BrickColor.new("Institutional white")
	trajectory.Material = "SmoothPlastic"
	trajectory.Name = player.Name.."'s Trajectory"
	trajectory.Transparency = 0.5
	trajectory.Anchored = true
	trajectory.Locked = true
	trajectory.CanCollide = false
	trajectory.Size = Vector3.new(0.3,0.3,distance)
	for i = 0,distance,6 do
		trajectory.CFrame = CFrame.new(tool.CFrame.p,position) * CFrame.new(0,0,-distance/2)
		wait(0.0001)
	end

	if part then
		if part.Name == "Head" or part:IsA("Hat") and part.Parent:FindFirstChild("Humanoid").Health > 0 then
			--headshot
			damage = 60
		end
		local humanoid = part.Parent:FindFirstChild("Humanoid")
		if not humanoid then
			humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
		else
			humanoid:TakeDamage(damage)
			if humanoid.Health <= 0 then
				print("dead lol")


			end
		end
		wait(0.00001)
		if trajectory then
			trajectory:Destroy()
		end
	end
end

end)
‘’’

1 Like

Just formatting your scripts so everyone can read it and help you:

(Right Gatling Gun)

local tool = script.Parent.RightGatlingGun --Getting the Tool
local player = game:GetService("Players").LocalPlayer --Getting the player
local mouse = player:GetMouse() --Getting the mouse
local difference = 0 --difference between head and mouse
local replicatedStorage = game:GetService("ReplicatedStorage")
local shootevent = replicatedStorage:WaitForChild("ShootEvent")

mouse.Button1Down:Connect(function()
	local head = game.Workspace[player.Name].Head.CFrame.lookVector -- finding players name and where they look
	local mouse = CFrame.new(game.Workspace[player.Name].Head.Position,mouse.Hit.p).lookVector
	difference = (head-mouse)
	local ray = Ray.new(tool.CFrame.p, (player:GetMouse().Hit.p - tool.CFrame.p).unit*300)
	local part,position = game.Workspace:FindPartOnRay(ray,player.Character,false,true)
	if difference.magnitude < 1.33 then
		shootevent:FireServer(tool,position,part)
	end
end)

Server Script:

local replicatedStorage = game:GetService("ReplicatedStorage")
local damage = 32

replicatedStorage.ShootEvent.OnServerEvent:Connect(function(player,tool,position,part)
	if game.Workspace[player.Name].Humanoid.Health <= 0 then
	else
		local distance = (tool.CFrame.p - position).magnitude
		if game.Workspace:FindFirstChild(player.Name.."s Trajectory") then
			game.Workspace:FindFirstChild(player.Name.."'s Trajectory"):Destroy()

		end
		local trajectory = Instance.new("Part",game.Workspace)
		trajectory.BrickColor = BrickColor.new("Institutional white")
		trajectory.Material = "SmoothPlastic"
		trajectory.Name = player.Name.."'s Trajectory"
		trajectory.Transparency = 0.5
		trajectory.Anchored = true
		trajectory.Locked = true
		trajectory.CanCollide = false
		trajectory.Size = Vector3.new(0.3,0.3,distance)
		for i = 0,distance,6 do
			trajectory.CFrame = CFrame.new(tool.CFrame.p,position) * CFrame.new(0,0,-distance/2)
			wait(0.0001)
		end

		if part then
			if part.Name == "Head" or part:IsA("Hat") and part.Parent:FindFirstChild("Humanoid").Health > 0 then
				--headshot
				damage = 60
			end
			local humanoid = part.Parent:FindFirstChild("Humanoid")
			if not humanoid then
				humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
			else
				humanoid:TakeDamage(damage)
				if humanoid.Health <= 0 then
					print("dead lol")
				end
			end
			wait(0.00001)
			if trajectory then
				trajectory:Destroy()
			end
		end
	end
end)
1 Like

looking at the formatted scripts, there are many deprecated things i couldn’t comprehend on but lets just ignore them, firstly you can edit the server script as such: (i can’t really properly indent because i’m lazy to switch to studio)

local ArmToShootFrom: BasePart -- link it to the arm you want it to shoot from

local function Shoot()

local SpreadOffset = Vector3.one * math.random() * 10 -- returns a random offset with the same x, y, and z
local Target = ArmToShootFrom.LookVector * 10 + SpreadOffset -- shoots anything in a 10 studs range from the arm with a random offset
local Distance = (ArmToShootFrom.Position - Target).Magnitude

local Trajectory -- i recommend cloning from a template to avoid the unnecessary clutter of setting the properties manually

-- .. more stuff

end

-- call Shoot whenver you want to shoot
Shoot()
1 Like

I will try this. Thanks! Would you know what to search to youtube to learn more about coding stuff like these?

1 Like

im not quite sure but you could make a small project and try to add the mechanics one by one, for example; try to figure out how to move a humanoid character or how do i find a path so that my bot can navigate through obstacles/mazes? if you get stuck you could always google it and try to not get trapped in the tutorial hell where you rely on tutorials without actually learning

2 Likes

For this post, is learning to code magnitude and velocity important? Thanks I will try what you said, are there any other coding terms or classes I need to learn to succeed in this?

2 Likes

practically yes, because you’re dealing with bullets which needs velocity to travel (you could use other alternatives that doesn’t use the physics engine velocity property for more accurate results);

im not sure but CFrame is a really complicated component and you probably need it a lot later on

1 Like

Thanks, dude you’re the best. Lotta help and appreciate it!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.