Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 7 years ago
Edited 7 years ago

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

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

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)
0
That doesn't work because the part is undefined whenever you do that. True_Warrior 29 — 7y
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. SkeletalReality 590 — 7y
Ad

Answer this question