Script just breaks?

I have this piece of code in a local script in starterplayerscripts

-- wait(2)
local player = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")
local key = Enum.KeyCode.F
local interact = player.PlayerGui:WaitForChild("Interact")
local gui = player.PlayerGui.ShopText
local text
local name
local shoptype

local mouse = player:GetMouse()

mouse.Move:Connect(function()
	local target = mouse.Target
	print(target)
	if target.Parent:FindFirstChild("Interactable") and target.Parent:FindFirstChild("Humanoid") and target.Parent.Interactable.Value == false and (target.Position - player.Character.Torso.Position).magnitude < 20 then
		interact.Enabled = true
		UIS.InputBegan:Connect(function(input)
			if input.KeyCode == key then
				text = target.Parent.Text.Value
				name = target.Parent.NPCName.Value
				shoptype = target.Parent.ShopType.Value
				target.Parent.Interactable.Value = true
				gui.Enabled = true
				gui.Frame.Text.Text = text
				gui.Frame.NPCName.Text = name
				gui.ShopType.Value = shoptype
			end
		end)
	else
		interact.Enabled = false
	end
end)

The script basically just opens a shop based on which NPC you interact with.

The first time it works, but then It just ignores all the ifs and it opens the chat when you press f anywhere at any time. Then proceeds to give me an error which says that “Interactable is not a valid member of Model.”

I then go check the NPC model, and Interactable is still there.

I don’t know whats wrong with the code or what to do so I thought to ask the forum.

1 Like

Its possible Interactable has not loaded in yet

and target.Parent.Interactable.Value
--change to
and target.Parent:WaitForChild("Interactable").Value
1 Like

It works at first so that cant be it,

I tried it anyway and as I thought it didn’t work.

1 Like

This makes a new connection every time the mouse moves. Not only will this be incredibly inefficient, but it also won’t stop checking for the key to be pressed when the target changes.

3 Likes

So how would I fix that, would I put the if statement inside of the InputBegan function?

probably its inefficient to do if statements outside events

I tried it, and it ended up with worse results

I made some adjustments and now it ends up the same as before, except the interact gui does not show.

Im even more confused now I don’t know why it cant find Interactable in the npc, and I dont’t know why it only works once.

What if you change the target.Parent.Interactable.Value back to false?
Since your first if statement looks for that and I don’t see it being changed back to false anywhere.

I guess I never really explained that sorry

In another script of the shop that is opened up when you interact with the npc, there is a script that does that when you close the shop

local button = script.Parent
local gui = button.Parent.Parent
local NPCs = game.Workspace:FindFirstChild(“NPCs”)
local npc = NPCs:FindFirstChild(“Test”)

button.MouseButton1Click:Connect(function()
gui.Enabled = false
npc.Interactable = false
end)

its inside of a button inside of the shop.

This is the script that doesnt find interactable in the npc model, have no idea why.

1 Like

It turns out I forgot to put Interactable.Value, I just used Interactable

I just wasted so much time for that thank you for making me realize that

2 Likes