Unable to resolve error despite script working as intended

Hey there!

I’m having difficulties with a script intended to close a frame when the player moves, which prevents them from interacting with the frame from a distance, resulting in it being abused. The script itself works, however returns the following error every step the player takes, regardless of whether or not the frame is open.

Players.DishwasherSushi.PlayerGui.BusTicketGui.CloseOnMoveScript:7: attempt to index nil with ‘Visible’

I have searched through similar posts, however I have yet to find one that relates to my issue and resolves it using the solutions marked on said posts. Below you can find all relevant information.

Pathway

image

Code

local player = game.Players.LocalPlayer
local frame = script.Parent

local function onPlayerMove()
	if frame.Visible == true then
		frame.Visible = false
	end
end

local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
	if humanoid.MoveDirection.Magnitude > 0 then
		onPlayerMove()
	end
end)```

Seems like the parent of the script changes somewhere. If you look closely at the error message, you can see that it isn’t under the Frame anymore, but under the ScreenGui.

Strange. Do you know what may be causing this, or how it could be prevented?

I wouldn’t know. However, you could try looking for code that moves the script using the Find All / Replace All tool.

  1. Change the name of th script . This allow you see which other script breaks when trying to parent the script to something else

  2. check if the script itself change’s its parent .

1 Like
  1. Nothing breaks when the name of the script is changed.
  2. The script itself doesn’t change its parent.

As an update as part of further investigation, the error only prints when the character’s direction is moved, not every time the character moves in general.

After looking even deeper into the issue, I used an if statement to return if the Frame is nill, and this resolved all of the errors that resulted from it.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.