Explosion positioning to a Part's position not working

Desired outcome:
I want to create a new Explosion object that has the same position as a Part whenever this Part is created.

Current outcome:
When the Part is created, the Explosion is created at a position of (0,0,0), instead of the Part’s position.

What I have tried in order to fix the issue:
I have watched YouTube tutorials regarding Explosion object positions related to Part positions and implementing “Explosion.position=Part.position” to no avail, as this does not work for me.
I have searched through the Developer Forum for a solution regarding Explosion object positions and
they use the same method listed above. Once again, “Explosion.position=Part.position” does not work for me.

Extra details:
The Part being created is referred to as “projectile” in the script.
The Explosion being created is referred to as “boom” in the script.
This might be a bug, although I am not fully sure.

This is my script. It is server-sided.

local tool=script.Parent
local handle=tool.Handle

local remotefunction=tool.RemoteFunction
local debris=game:GetService("Debris")

function remotefunction.OnServerInvoke(player,command,value)
	if command=="lightning" then
		local projectile=Instance.new("Part",workspace)
		projectile.Anchored=true
		projectile.CanCollide=false
		projectile.Size=Vector3.new(5,5,5)
		projectile.BrickColor=BrickColor.new("Really black")
		projectile.Shape=Enum.PartType.Ball
		projectile.Transparency=.8

		local boom=Instance.new("Explosion",projectile)
		boom.Position=projectile.Position --Does not work
		boom.Visible=true
		
		projectile.CFrame=CFrame.new(value+Vector3.new(0,0,0))
		debris:AddItem(projectile,1)
		return projectile
	elseif command=="hit" then
		if value[1] then
			value[1]:TakeDamage(value[2])
			for _,instance in pairs(value[1]:GetChildren()) do
				if instance.Name=="creator" then
					instance:Destroy()
				end
			end
			local creator=Instance.new("ObjectValue",value[1])
			creator.Name="creator"
			creator.Value=player
		end
	end
end

I am a novice at scripting, so this might be a very easy fix that I do not know :clown_face:.

2 Likes

The problem is that you have to set the explosions parent to workspace. It is setting the position to the projectile, your just not seeing it because it isn’t parented to the workspace.

Just tried this, but the Explosion is still being created at (0,0,0).
The output window doesn’t see any errors in the script, which is odd.

Also, how does the parent even affect the position of the Explosion when Explosion objects do not replicate the Parent position? The reason I had the parent as Projectile was so the Explosion would be destroyed when Projectile was destroyed by “debris:AddItem(projectile,1)”. I just did that to optimize.

Why don’t you try diffrent positions and see if they work or not? Maybe it’s get you somewhere.

I am not trying to position the Explosion anywhere other than the Part’s position.
The goal is that when a new Part is created, a new Explosion is created at the new Part’s exact same position.
However, when I set the Explosion’s position equal to the Part’s position, it is created at (0,0,0) instead.

Could you elaborate on what you are suggesting?

When Creating an instance it replicates the data that instance
So after all the Properties are changed sets its parent.

local boom=Instance.new("Explosion")
boom.Position=projectile.Position
boom.Visible=true
boom.Parent=projectile

This did not help. When I used this, the explosion did not change position. What exactly does this accomplish?

If you print(projectile.Position, " ", boom.Position) right before the explosion it’ll tell you the values for troubleshooting purposes.

I could be completely wrong, I just remember seeing something like this in other posts. I think it’s because you can’t use Position to CFrame an object. You have to set it using a CFrame.

image
Is this what you’re referring to?

maybe Put this infront of the cframe change

Flip Flop this to

projectile.CFrame=CFrame.new(value+Vector3.new(0,0,0))
debris:AddItem(projectile,1)

local boom=Instance.new("Explosion")
boom.Position=projectile.Position
boom.Visible=true
boom.Parent=projectile

return projectile

I see in the projectile part creation code that you’re not changing its position from its default (0,0,0) until after the explosion’s been created. Because the part has not yet been positioned properly, the explosion uses the part’s default position and goes off at 0,0,0. Also, you shouldn’t use the second argument of Instance.new() to parent created instances. Parenting new instances should always come last (explanation here).

local Projectile = Instance.new("Part")
Projectile.Anchored = true
Projectile.CanCollide = false
Projectile.Size = vector3.new(5,5,5)
Projectile.BrickColor = BrickColor.new("Really Black")
Projectile.Shape = Enum.PartType.Ball
Projectile.Transparency = 0.8
Projectile.CFrame = CFrame.new(value + Vector3.new(0,0,0)) --// idk what's going on here but ok then
Projectile.Parent = workspace

local Explosion = Instance.new("Explosion")
Explosion.Position = Projectile.Position
Explosion.Parent = workspace

debris:AddItem(Projectile,1)
return Projectile
2 Likes

I’m not sure how this fixed it exactly, could you elaborate more for my small brain? Sorry.
This does work though! Thank you for the solution to my problem.

Think of the code as a list of instructions followed from top to bottom (and from left to right if several instructions are on one line). The order in which these instructions are carried out can be very important to ensuring later instructions and ultimately the entire script bring forth the desired result.

The important line in your script is Projectile.CFrame = CFrame.new(value + Vector3.new(0,0,0)), which I’ll call SetPosLine for this explanation. Because the position of the explosion is dependent on the position of the projectile part, the script needs to execute the instruction SetPosLine before it creates the explosion and sets its position. Your original code had SetPosLine come after the code to create and configure the explosion, which was causing the explosion to use the projectile part’s old position 0,0,0 because SetPosLine had not yet been reached to change it. In my post, I simply fixed the faulty order of operations.

1 Like

This makes much more sense. I appreciate the guidance.

1 Like

I mean’t to put some other positions to see what was wrong, So you can see if the problem is in that specific parts position. Doing this may get you somewhere to figuring the solution easily. That’s what I meant by it.

1 Like

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