Script kind of doesnt work

hello, so im trying to achieve a rpg in which when you shoot, for example, a part thats in a folder, then it unanchors that part, if you hit the terrain, it will just explode where it got hit., the problem is that i only managed to get the folder destruction to work, and it still doesnt even unanchor

local ReplicatedStorage = game:GetService(‘ReplicatedStorage’)
local remoteEvent = ReplicatedStorage:WaitForChild(‘RPG’)
local hit = ReplicatedStorage.Hit
local DamageToDoIfTheHitIsAPlayer = 0

remoteEvent.OnServerEvent:Connect(function(player, gunPos, gunOr, mosPos)
game:GetService(“ReplicatedStorage”).Launch:Play()
local bullet = Instance.new(“Part”)
bullet.Position = gunPos
bullet.Orientation = gunOr
bullet.Transparency = 0
bullet.Name = ‘Bullet’
bullet.Parent = game.Workspace
bullet.Size = Vector3.new(.25, .25, .25)
bullet.BrickColor = BrickColor.new(‘Neon orange’)
bullet.Shape = Enum.PartType.Block
bullet.CanCollide = true

local speed = 500
bullet.CFrame = CFrame.new(gunPos, mosPos)
bullet.Velocity = bullet.CFrame.lookVector * speed

bullet.Touched:Connect(function(otherPart)
	local player = otherPart.Parent.Parent:FindFirstChild("Humanoid")
	if not player then
		local folder = otherPart.Parent:IsA("Folder")
		if folder then
			local explosion = Instance.new('Explosion')
			explosion.Name = "EXPLOSION"
			explosion.Parent = workspace
			explosion.BlastRadius = 4
			explosion.BlastPressure = 500000
			explosion.DestroyJointRadiusPercent = 1
			explosion.ExplosionType = Enum.ExplosionType.Craters
			explosion.TimeScale = 1
			explosion.Position = bullet.Position
			explosion.Hit:Connect(function(Hit)
				if Hit.Parent:IsA("Folder") then
					Hit.Anchored = false
				end
			end)
		end
	end
end)

end)

1 Like

You should format your code post with ```

anyway, i’ll give it a shot…
why are you connecting to explosion.hit ?? you already used the touched:connect to figure out that you hit something and you have the object: otherPart

just set the otherPart.Anchored = false

why are you checking that the hit.parent is a Folder, you already checked that, but you don’t need that connection anyway so just get rid of it.

also you shouldn’t parent your explosion to workspace till after you have set all the properties of it.

image

1 Like

im connecting to explosion.hit so i can detect if somethings in the blast radius, and if it is, then it unanchores the certain part

You can’t re-use the Hit variable here. Change it to part and it should work:

			explosion.Hit:Connect(function(part)
				if part.Parent:IsA("Folder") then
					part.Anchored = false
				end
			end)