Hello guys i just wanted to create tower defense but explosion spawning at same position but not tower position here is the code:

Hello guys i just wanted to create tower defense but explosion spawning at same position but not tower position here is the code:

local meganima = script.Parent.Humanoid.Animator:LoadAnimation(script.Parent.idle)
meganima.Looped = true
meganima:Play(0,9,0.1)
meganima.Ended:Connect(function()
meganima:Play(0,9,0.1)
end)

function givemoneytoowner()
local animaska = script.Parent.Humanoid.Animator:LoadAnimation(script.Parent.atack)
animaska.Looped = false
animaska:Play(0,9,0.5)
script.Parent.creator.Value.leaderstats[“Spare parts”].Value+=100
end
local sigomka = 0
while true do
if script.Parent and workspace:FindFirstChild(“Enemies”) then
if sigomka < 5 then
sigomka+=1
givemoneytoowner()
elseif sigomka >=5 then
local bybym =Instance.new(“Explosion”,workspace)
bybym.Position = script.Parent.ExplodingBarrel.Head.Position
bybym.BlastPressure = 0
DamageAllTargetsInRange()
sigomka = 0
end
end
wait(0.5)
end

If you format the code it’s much easier to read it etc like this

local hello = "world"

Anyways have you tried setting the position before making workspace the parent? I think when you set the explosion in workspace it goes off instantly

Parent the explosion inside the “Tower” instead :+1:

1 Like

i parented explosion to tower and it still spawning in position that tower was in server storage

Please format your code with ``` ← these thingies :​D

Anyways, where is this script located? Are there any errors in the output?
I’m thinking script.Parent.ExplodingBarrel (or Head in it) doesn’t exist, not setting the Position.
Could you put;

print(script.Parent.ExplodingBarrel)
print(script.Parent.ExplodingBarrel.Head)

before creating the explosion to see if any of them are nil?

Also, doing Instance.new(class, Parent) is bad for performance, so i recommend setting the parent after creating the explosion

if there werent object there will be errors in output but there is no errors and explosion spawning isnt in workspace position

Try positioning it before parenting it.

			local bybym = Instance.new("Explosion")
			bybym.Position = script.Parent.ExplodingBarrel.Head.Position
			bybym.BlastPressure = 0
			bybym.Parent = workspace
			DamageAllTargetsInRange()
			sigomka = 0