What solutions have you tried so far?
I’ve tried doing while wait() do render stepped and all sorts of methods.
w
local UserInputService = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
local Clone = script.Parent.Folder.Keys
UserInputService.InputBegan:Connect(function(keycode)
for i,v in pairs(game.Workspace.Folder:GetChildren()) do
if (v.NonTween.Center.Position - Player.Position).magnitude <= 6 then
if keycode.KeyCode == Enum.KeyCode.F then
if Clone.Adornee == v.NonTween.Center then
v.Main.RemoteEvent:FireServer()
end
end
end
end
end)
while wait() do
for i,v in pairs(game.Workspace.Folder:GetChildren()) do
if v:IsA("Model") then
if (v.NonTween.Center.Position - Player.Position).magnitude <= 6 then
Clone.Adronee = v.NonTween.Center
Clone.Enabled = true
else
Clone.Adronee = script.Parent.Folder
Clone.Enabled = false
end
end
end
end
I’m not sure what you tried to achieve with making a loop arround your function. That would only end in making your game more laggy every time the loop runs and I guess you tried that already without loop but it didn’t work as intended. Looping the UserInputService.InputBegan function would end in that you have after 10 sec like 100 events which fire if you press a button but one time is enough. That’s how i would do it:
local UserInputService = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
local Clone = script.Parent.Folder.Keys
local AdorneedDoor = nil
UserInputService.InputBegan:Connect(function(keycode)
if keycode.KeyCode == Enum.KeyCode.F then
if AdorneedDoor then
if Clone.Adornee == AdorneedDoor.NonTween.Center then
AdorneedDoor.Main.RemoteEvent:FireServer()
end
end
end
end)
while true do
for i,v in pairs(game.Workspace.Folder:GetChildren()) do
if (v.NonTween.Center.Position - Player.Position).magnitude <= 6 then
Clone.Adornee = v.NonTween.Center -- << this part of the script is not working
Clone.Enabled = true -- << this part of the script is not working
AdorneedDoor = v
break
else
Clone.Adornee = script.Parent.Folder -- << this part of the script is not working
Clone.Enabled = false -- << this part of the script is not working
AdorneedDoor = nil
end
end
wait()
end
Good you’re asking the problem was the for loop of your doors folder. I guess you know that the for loop runs everything inside of it and as it hits it end it runs again. Explained easily Door1 is running first in the loop and you’re 6 Studs nearby it than it shows you the adorneed UI but after it ran the code inside the loop there comes Door2 which isn’t 6 Studs near you and it switches the UI off. And the break after switching the UI on simply prevents that other Doors that would get checked in the loop, if you’re near enough them, don’t switch your UI off because you’re too far away from them.Thats the easy but very long way of how I would explain it.