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

How to disable script when a part is not touched?

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

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

3 comments

0

Still says the same

0

hit.parent might not be the character check if it is the players character first?

0

now it says: Workspace.Plot.Disabler:7: attempt to index nil with 'Animations'

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)

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