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

Why does my ability (sometimes) stop working if I die while it is in effect?

Asked by 5 years ago

I have scripted an Ability that I called Rainbow path.

It allows the player to spawn random color platforms underneath their feet for a few seconds, effectively allowing them to walk on air for the duration of the ability.

It's a local script that I put in StarterGUI then moved to Replicated first. This seems to make no difference. Are either of these the right place to put such a script?

Here is the localscript: (Sorry if the formatting is off, I'm still a noob to script helpers site.)

--RainbowPath

local done = false

function RainbowPath(actionName, userInputState, inputObject)

userInputState = Enum.UserInputState.Begin
if done == false then

    done = true

    for i = 1, 20 do
        local Player = game.Players.LocalPlayer
        local Path = Instance.new('Part', game.Workspace)
        Path.Name = "Path" .. tostring(i)
        Path.BrickColor =           BrickColor.new(math.random(),math.random(),math.random())
        Path.Anchored = true
        Path.Size = Vector3.new(5,.3,3)
        Path.Material = "Neon"
        Path.CFrame = Player.Character.Head.CFrame * CFrame.new(0,-4.7,0)
        wait(.1)

    end
    for i = 1, 20 do
        local Path = game.Workspace:FindFirstChild("Path" .. tostring(i))
        Path.Anchored = false
        wait(.05)
        Path.Transparency = 0.7
        wait(.05)
        Path:Destroy()

    end
    wait(0.5)
    done = false
 end

end

game.ContextActionService:BindAction("RainbowPath", RainbowPath, false, Enum.KeyCode.R)

So the ability works great. However, if the player dies while the ability is in effect it stops working (sometimes). As in the player will no longer be able to use the ability until they leave and re-enter the game. Again this only happens sometimes but is still consistent enough to be considered an issue.

Some one please help, I've tried a bunch of things but nothing seems to work. I am not skilled enough of a programmer to be able to re-script this to be bug-free, so I would love it if an expert can help me out.

Thanks in advance.

1 answer

Log in to vote
0
Answered by
qChaos 86
5 years ago

It's because the player's character (and head) doesn't always exist, to solve this, simply wait for the character and head if they don't exist.

local Character=Player.Character or Player.CharacterAdded:Wait()
local Head=Character:WaitForChild'Head'
0
Thank you so much! Do I add both lines inside of the for loop? CodeREVKids 5 — 5y
Ad

Answer this question