Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How would I make the "LightExplosion" part fling after its first instance?

How come this script only flings for the first instance of "LightExplosion" In another script a ball named lightexplosion is created in the workspace and then after a few seconds it disappears, so there will never be more than one "LightExplosion" in the server at a time. It only flings the first time it is touched, but when it disappears and another lightexplosion is made it doesn't work on the second one. Here's the script for the fling:

-- Adds a repulsive force to anything this object touches
local Part = game.Workspace:WaitForChild("LightExplosion")

local Debris = game:GetService('Debris')

local CharacterToIgnore = script:WaitForChild('CharacterToIgnore').Value

local MAGNITUDE = 3E4
local TIME_OF_FORCE = 0.2

Part.Touched:connect(function(other)
	if other.Parent == CharacterToIgnore or (other.Parent and other.Parent.Parent == CharacterToIgnore) then return end
	if not other.Anchored then
		local punchSound = script:FindFirstChild('PunchSound')
		if punchSound then punchSound:Play() end
		local direction = (other.Position - Part.Position).unit
		local bodyForce = Instance.new('BodyForce')
		bodyForce.force = MAGNITUDE * direction
		bodyForce.Parent = other
		Debris:AddItem(bodyForce, TIME_OF_FORCE)
	end
end)
	

1 answer

Hi!

As far as I'm concerned there is no errors in your fling script. The issue is that the Part variable does not update itself when you put a new LightExplosion in the workspace. Therefore, you must put the variable inside the function. This is so the variable can update and find the new, replaced LightExplosion part.

-- Adds a repulsive force to anything this object touches
local Part = game.Workspace:WaitForChild("LightExplosion")

local Debris = game:GetService('Debris')

local CharacterToIgnore = script:WaitForChild('CharacterToIgnore').Value

local MAGNITUDE = 3E4
local TIME_OF_FORCE = 0.2

Part.Touched:connect(function(other)
Part = game.Workspace:WaitForChild("LightExplosion") --Variable goes here!
    if other.Parent == CharacterToIgnore or (other.Parent and other.Parent.Parent == CharacterToIgnore) then return end
    if not other.Anchored then
        local punchSound = script:FindFirstChild('PunchSound')
        if punchSound then punchSound:Play() end
        local direction = (other.Position - Part.Position).unit
        local bodyForce = Instance.new('BodyForce')
        bodyForce.force = MAGNITUDE * direction
        bodyForce.Parent = other
        Debris:AddItem(bodyForce, TIME_OF_FORCE)
    end
end)

2 comments

0

That doesn't work because the part is undefined whenever you do that.

0

Wait oops sorry I still need the Part variable at the top to define the function. I edited it, try it now and see if it works.

This discussion is preserved as it was. New answers, comments, and votes are closed.