So I'm trying to make a tool that simply fires a bullet towards the mouse.

Localscript (in the tool)

local tool = script.Parent
local event = tool:WaitForChild("CreatePartEvent")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

script.Parent.Activated:Connect(function()
	local hit = mouse.Hit
	local part = Instance.new("Part")
	local speed = 100
	part.Size = Vector3.new(5,.25,2)
	part.CFrame = CFrame.new(player.Character.LeftHand.Position, mouse.Hit.p)
	part.CanCollide = false
	part.Velocity = part.CFrame.LookVector * 100
	part.Name = "Bullet"
	part.Transparency = 0.25
	local bf = Instance.new("BodyForce")
	bf.Force = Vector3.new(0, game.Workspace.Gravity, 0) * part:GetMass()
	bf.Parent = part
	event:FireServer(part)
end)	

Serverscript (also in the tool)

local tool = script.Parent
local event = Instance.new("RemoteEvent")
event.Name = "CreatePartEvent"
event.Parent = script.Parent

event.OnServerEvent:Connect(function(player, part)
	part.Parent = game.Workspace
	part.Touched:Connect(function(toucher)
		if toucher.Parent ~= player.Character and toucher.Parent.Parent ~= player.Character then
			part:Destroy()
		end
	end)
end)

From what I understand about this stuff, it should work, except whenever I click I get this error: Workspace.Tool.Script:7: attempt to index nil with 'Parent'

As much as I would like to create the bullet entirely server sided there's a delay between when the bullet is shown and when the bullet starts moving, which I'd like to avoid.

2 comments

0

Hi could you show the server script to the end of its function I can't see line 20 of it I cant really understand the problem..

0

My bad, I shortened it down a bit so that only what was relevant was shown. It would be line 7 that creates the issue