Help on making a tool that spawns a model infront of the player

Hello, I am trying to make a tool that spawns a sentry gun model infront of the player, but I am having trouble on making it work, when I use the tool, the sentry gun does not spawn, and it gives me this error:

HumanoidRootPart is not a valid member of Tool "Players.Wultramn.Backpack.SentryGun" - Server - ToolScript:16

I can’t quite figure out how to fix the problem.

Here’s the script:

local SentryGun = game:GetService("ReplicatedStorage"):WaitForChild("SentryGun")
local PrimaryPart = SentryGun.PrimaryPart

local Character = script.Parent

local Params=RaycastParams.new()
Params.FilterType=Enum.RaycastFilterType.Blacklist
Params.FilterDescendantsInstances={Character, SentryGun} 
Params.IgnoreWater=true

local Origin=Character.HumanoidRootPart.CFrame*CFrame.new(Vector3.new(0,0,-3))
Origin=Origin.Position
local Raycast=workspace:Raycast(Origin,Vector3.new(0,-100,0),Params)
if Raycast then
	local Position=Raycast.Position
	local InstanceHit=Raycast.Instance
	local P=Vector3.new(Position.X,InstanceHit.Position.Y+(InstanceHit.Size.Y/2)+(PrimaryPart.Size.Y/2),Position.Z)
	PrimaryPart.CFrame=CFrame.new(P,Character.HumanoidRootPart.CFrame.LookVector+P)
	SentryGun.Parent=workspace
end

Please help

Hey @Wultramn !

I believe the error may be caused by when you defined Character at the beginning of your script. The path is leading to the tool and not the player’s character.

The correct path to the character would be (assuming the script is directly inside the tool):

local Character = script.Parent.Parent

I hope this helps, and let me know if you’re experiencing any other issues.

1 Like

Wow this looks great so far. One thing that really helps with debugging problems is prints. This is a tool right… probably shouldn’t try to define HumanoidRootPart until it’s equipped. At that point the Character is right where League mentioned.

1 Like

Hmmm… That didn’t quite fix it… Sentry Turret still doesn’t spawn

Players.Wultramn.Backpack.SentryGun.ToolScript:9: attempt to index nil with 'Parent'

Unless I messed it up…

local SentryGun = game:GetService("ReplicatedStorage"):WaitForChild("SentryGun")
local PrimaryPart = SentryGun.PrimaryPart

-- Make Sure that the Model has A Primary Part Set
-- Anchor the PrimaryPart, Unanchor All the Other Parts
-- SetCanCollide of All Parts to true
-- Weld the Other Parts to the Primary Part Using Weld Constraints

local Character = script.Parent.Parent

--Rest of the script goes here

also sorry for the late response