I’m working on a Sci-Fi sort of game and this is my first big project. In this game I thought it would be a nice idea to make every intractable object be binded to a key/Button (GUI button for mobile users once I get to that point). The problem I face is all said objects activate at once, instead of whichever is closest. I have it set up so that the objects should only be activated when the player is within a minimal distance to them, however it only does it with one door and none of the others.
local player = game:GetService("Players").LocalPlayer
local character = player.Character
local RunService = game:GetService("RunService")
local Events = game:GetService("ReplicatedStorage").Events
while not player.PlayerGui:FindFirstChild("HUD") do
wait()
end
local HUD = player.PlayerGui.HUD
local Object_radius = 10
local ObjectInfo = {
ObjectNear = false,
ObjectName = "",
}
RunService.Stepped:Connect(function(step)
for _,v in pairs (workspace:GetDescendants()) do
if v:IsA("Model") and v:FindFirstChild("Config") then
if v.Config:FindFirstChild("Interactable") and v:WaitForChild("Base") then
if player:DistanceFromCharacter(v:GetPrimaryPartCFrame().p) < Object_radius then
--player.PlayerGui.HUD.Interact.TextLabel.Text = v.Config.Interactable.Controls.Interact.Value
local Cam = workspace.CurrentCamera:WorldToScreenPoint(v:GetPrimaryPartCFrame().p)
HUD.Interact.Visible = true
HUD.Interact.Position = UDim2.new(0,workspace.CurrentCamera:WorldToScreenPoint(v:GetPrimaryPartCFrame().p).X,0,workspace.CurrentCamera:WorldToScreenPoint(v:GetPrimaryPartCFrame().p).Y)
if v.Config:FindFirstChild("Open") then
if not v.Config.Open.Value then
HUD.Interact.InteractText.Text = "Open"
else
HUD.Interact.InteractText.Text = "Close"
end
end
if v.Config:FindFirstChild("Locked") then
HUD.Lock.Visible = true
HUD.Lock.Position = UDim2.new(0,Cam.X,0,Cam.Y)
if v.Config.Locked.Value then
HUD.Lock.LockText.Text = "Unlock"
else
HUD.Lock.LockText.Text = "Lock"
end
end
ObjectInfo.ObjectNear = true
ObjectInfo.ObjectName = v.Name
--print(ObjectInfo.ObjectName)
--print(ObjectInfo.ObjectNear)
else
--player.PlayerGui.HUD.Interact.TextLabel.Text = ""
HUD.Interact.Visible = false
HUD.Lock.Visible = false
HUD.Lock.LockText.Text = "Lock"
HUD.Interact.InteractText.Text = "Interact"
ObjectInfo.ObjectNear = false
ObjectInfo.ObjectName = ""
end
end
end
end
end)
game:GetService("UserInputService").InputBegan:Connect(function(input, gameProcessedEvent)
if not gameProcessedEvent then
if input.UserInputType == Enum.UserInputType.Keyboard then
if ObjectInfo.ObjectNear then
if input.KeyCode == Enum.KeyCode.E then
Events.Interact:FireServer(false)
elseif input.KeyCode == Enum.KeyCode.R then
Events.Interact:FireServer(true)
end
end
end
end
end)
The code is a bit messy, but I plan on cleaning it up once I can fix the issue.
I’m not quite sure why or how it happens. I tried figuring it out with no avail, and I can’t find anything about it online. If anyone could help me see what the problem is, I would greatly appreciate it.
Thank you in advance.