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

How to disable script when a part is not touched?

Asked by 2 years ago

What I'm trying to do: Enable the script if I stay on the platform and disable the script if I leave the platform(part).

local Player = game:GetService("Players")
local debounce = false

function Touched(hit)
    if debounce == false then
        local player = Player:GetPlayerFromCharacter(hit.Parent)
        player:FindFirstChild("StarterGui").Animations.AnimationList.Animationcontroller.Disabled = false
        debounce = true
    end
end

function TouchEnded(hit)
    if debounce == true then
        local player = Player:GetPlayerFromCharacter(hit.Parent)
        player:FindFirstChild("StarterGui").Animations.AnimationList.Animationcontroller.Disabled = true
        debounce = false
    end
end

script.Parent.Touched:Connect(Touched)
script.Parent.TouchEnded:Connect(TouchEnded)

But the studio says:

Workspace.Plot.Disabler:7: attempt to index nil with 'FindFirstChild'

I don't know what to do any suggestions?

2 answers

Log in to vote
0
Answered by 2 years ago

The players gui isn't called StarterGui. It is called PlayerGui. StarterGui is the gui players are given upon joining the game.

0
Still says the same 123balint1 -15 — 2y
0
hit.parent might not be the character check if it is the players character first? EnzoTDZ_YT 275 — 2y
0
now it says: Workspace.Plot.Disabler:7: attempt to index nil with 'Animations' 123balint1 -15 — 2y
Ad
Log in to vote
0
Answered by 2 years ago

So I figured out the findFirstchild problem but the script keeps rapidly enable/disable when I touch the part(Plot).

local Player = game:GetService("Players")
local debounce = false

function Touched(hit)
    if debounce == false then
        local player = Player:GetPlayerFromCharacter(hit.Parent)
        game:GetService("StarterGui").Animations.AnimationList.Animationcontroller.Disabled = false
        debounce = true
    end
end

function TouchEnded(hit)
    if debounce == true then
        local player = Player:GetPlayerFromCharacter(hit.Parent)
        game:GetService("StarterGui").Animations.AnimationList.Animationcontroller.Disabled = true
        debounce = false
    end
end

script.Parent.Touched:Connect(Touched)
script.Parent.TouchEnded:Connect(TouchEnded)

Answer this question