Workspace.Part.Script:42: attempt to index nil with 'Character'

  1. What do you want to achieve? Keep it simple and clear!
    I am trying to make a rocket script for an RPG.
  2. What is the issue? Include screenshots / videos if possible!
    The error I am getting is: 12:47:06.465 Workspace.Part.Script:42: attempt to index nil with 'Character' - Server - Script:42.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I looked for DevForum solutions and none seem to work.
local Players = game:GetService("Players")
local Debris = game:GetService("Debris")

local rocket = script.Parent
local CreatorTag = rocket:FindFirstChild("Creator")
local Player = CreatorTag.Value
local explosion

local BlastRadius = 8
local IgnoreList = {rocket = true, handle = true, effect = true, water = true}
--NOTE: Keys must be lowercase, values must evaluate to true

function IsTeamMate(p1, p2)
	return (p1 and p2 and not p1.Neutral and not p2.Neutral and p1.Team == p2.Team)
end

local function ApplyTags(target)
	while target:FindFirstChild('creator') do
		target.creator:Destroy()
	end

	local creatorTagClone = CreatorTag:Clone()
	Debris:AddItem(creatorTagClone, 1.5)
	creatorTagClone.Parent = target
end

local function IsInTable(Table,Value)
	for _,v in pairs(Table) do
		if v == Value then
			return true
		end
	end
	return false
end

local TaggedHumanoids = {}
local function onHit(hitPart, hitDistance)
	if hitPart and hitDistance then
		local character = hitPart.Parent
		if character ~= nil and character ~= Player.Character then -- Error occurs here.
			local humanoid = character:FindFirstChild("Humanoid")
			local player = Players:GetPlayerFromCharacter(character)
			
			if player and IsTeamMate(player, Player) then
				return
			end
			if humanoid then
				if not IsInTable(TaggedHumanoids,humanoid) then
					print("Tagged")
					table.insert(TaggedHumanoids,humanoid)
					ApplyTags(humanoid)
					humanoid:TakeDamage(400/3)
				end
			else
				if not hitPart.Parent:IsA("BackpackItem") then
					hitPart:BreakJoints()
					local VectorForce = Instance.new("VectorForce", hitPart)
					local Attachment = Instance.new("Attachment", hitPart)
					--NOTE: We will multiply by mass so bigger parts get blasted more
					
					VectorForce.Attachment0 = Attachment
					VectorForce.Force = (hitPart.Position - explosion.Position).unit * 1000 * hitPart:GetMass()
					
					-- AddDebris
					local lifetime = 0.1
					Debris:AddItem(VectorForce, lifetime)
					Debris:AddItem(Attachment, lifetime)
				end
			end
		end
	end
end

local function onTouched(hit)
	if hit then
		if IgnoreList[string.lower(hit.Name)] then
			return
		end
		if hit == workspace.Terrain then
			local frontOfRocket = rocket.Position + (rocket.CFrame.lookVector * (rocket.Size.Z / 2))
			local cellLocation = hit:WorldToCellPreferSolid(frontOfRocket)
			local ray = Ray.new(frontOfRocket, cellLocation)
			local _, _, _, material = workspace:FindPartOnRayWithWhitelist(ray, {hit}) -- part, position, ignorewater
			if material == Enum.Material.Water or material == Enum.Material.Air then
				return
			end
		end
		if hit.CanCollide then
			explosion = Instance.new("Explosion") do
				explosion.Visible = true
				explosion.BlastPressure = 0
				explosion.BlastRadius = BlastRadius
				explosion.DestroyJointRadiusPercent = BlastRadius * 0
				explosion.ExplosionType = Enum.ExplosionType.NoCraters
				explosion.Position = rocket.Position
				explosion.Parent = workspace
				
				
				
				explosion.Hit:Connect(onHit)
				
				script.Parent = explosion
				CreatorTag.Parent = script
				
			end
		end
	end
end


rocket.Touched:Connect(onTouched)

Player is nil. That’s why you’re getting that error. Looks like CreatorTag.Value is nil at the time you assign it to Player

1 Like

Thank you. I’ll try what you said in this topic.

Now, I’m having a different problem.

robloxapp-20240723-1541130.wmv (2.0 MB)

local Debris = game:GetService("Debris")

local tool = script.Parent
local _event = tool:WaitForChild("RemoteEvent")
local debounce = false

tool.Equipped:Connect(function()
	_event.OnServerEvent:Connect(function(player, cf)
		if not debounce then
			debounce = true
			local rocket = Instance.new("Part") do
				-- Setup the rocket.


				rocket.Locked = true
				rocket.Name = "Rocket"
				rocket.Parent = workspace
				rocket:PivotTo(cf)
				rocket.Size = Vector3.new(1, 1, 3.25)
				rocket.Anchored = false
				rocket.CanCollide = false

				rocket.BackSurface = Enum.SurfaceType.Smooth
				rocket.BottomSurface = Enum.SurfaceType.Smooth
				rocket.FrontSurface = Enum.SurfaceType.Smooth
				rocket.LeftSurface = Enum.SurfaceType.Smooth
				rocket.RightSurface = Enum.SurfaceType.Smooth
				rocket.TopSurface = Enum.SurfaceType.Smooth

				-- Add fire
				local fire = Instance.new('Fire', rocket)
				fire.Heat = 5
				fire.Size = 2

				-- Attach creator tags to the rocket early on
				local creatorTag = Instance.new('ObjectValue', rocket)
				creatorTag.Value = player
				creatorTag.Parent = rocket
				creatorTag.Name = 'creator' --NOTE: Must be called 'creator' for website stats
				local iconTag = Instance.new('StringValue', creatorTag)
				iconTag.Value = tool.TextureId
				iconTag.Name = 'icon'

				-- Finally, clone the rocket script and enable it
				local rocketScript = script.Script:Clone()
				rocketScript.Parent = rocket
				rocketScript.Enabled = true
			end
			
			wait(15)
			debounce = false
		end
	end)
	
	
end)