I am attempting to create a set of ovens that open up and a GUI that will pop-up when within a couple of studs. I cannot seem to figure out why it will only work for the first oven and not the second.
I have tried changing it from for _,Door in pairs(workspace.OvenDoors:GetChildren()) to individually naming both ovens. This was a bad idea because then the script broke. I also tried combining the functions to see if that would make a difference, but that also broke the script.
Everyone I have asked so far about this not working has not seemed to figure out the problem. The output does not print out any errors.
Here is a video that shows what happens in-game and in studio: https://vimeo.com/430734772
This code is inside of a local script within a ScreenGUI.
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
game:GetService("ContextActionService")
for _,Door in pairs(workspace.OvenDoors:GetChildren()) do
mouse.Move:Connect(function()
mouse.TargetFilter = Door
if mouse.TargetFilter == Door then
local Mag = (Door.Center.Position-game.Players.LocalPlayer.Character.HumanoidRootPart.Position).magnitude
if Mag <= Door.Range.Value then
print(mouse.TargetFilter)
script.Parent.TextLabel.Visible = true
else
script.Parent.TextLabel.Visible = false
end
end
end)
end
function onKeyPress(actionName, userInputState, inputObject)
if userInputState == Enum.UserInputState.Begin then
print("E pressed x")
for _,Door in pairs(workspace.OvenDoors:GetChildren()) do
local Mag = (Door.Center.Position-game.Players.LocalPlayer.Character.HumanoidRootPart.Position).magnitude
if Mag <= Door.Range.Value then
Door.Event:FireServer()
break
end
end
end
end
game.ContextActionService:BindAction("keyPressSpecialName", onKeyPress, true, Enum.KeyCode.E)
game.ContextActionService:SetPosition("keyPressSpecialName", UDim2.new(.5,0,-.5,0))
