What do you want to achieve? Keep it simple and clear!
I am trying to make a rocket script for an RPG.
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.
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)