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.
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.
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.
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”)