So I'm making my own interact gui and I want the script to clone the gui when the player is close enough to the part. This works when there id 1 player in the server, but when there is 2 or more players the gui keeps flashing. What I want to know is how i should define the player. Here is the script inside the part (server sided):
local maxdistance = 7
wait(.1)
while true do
wait()
if script.Parent:IsA("BasePart") then
for i,plr in pairs(game.Players:GetPlayers()) do
local distance = plr:DistanceFromCharacter(script.Parent.Position)
if distance < maxdistance then
ui = plr.PlayerGui:FindFirstChild("InteractUI")
if ui == nil then
local clone = script.InteractUI:Clone()
clone.InteractFrame.ITitle.Text = "<u><b>Ingredients</b></u>"
clone:SetAttribute("Type", script.Parent:GetAttribute("Type"))
clone:SetAttribute("InstanceName", script.Parent.Name)
if script.Parent.Name == "Bin" then
clone.InteractFrame.IngredientsFrame.Visible = false
clone.InteractFrame.ITitle.Visible = false
clone.InteractFrame.Title.Position = UDim2.new(0.255, 0,0.441, 0)
clone.InteractFrame.InteractButton.Description.Text = "To throw away ingredient"
end
if script.Parent:GetAttribute("Type") == "Fridge" then
clone.InteractFrame.IngredientsFrame.IngredientImage2.Visible = false
clone.InteractFrame.IngredientsFrame.IngredientImage3.Visible = false
clone.InteractFrame.InteractButton.Description.Text = "To get ingredient"
clone.InteractFrame.ITitle.Text = "<u><b>Ingredient</b></u>"
end
if script.Parent:GetAttribute("Type") == "Counter" then
clone.InteractFrame.IngredientsFrame.Visible = false
clone.InteractFrame.ITitle.Visible = false
clone.InteractFrame.Title.Position = UDim2.new(0.255, 0,0.441, 0)
clone.InteractFrame.InteractButton.Description.Text = "To place/grab ingredient"
end
clone.Parent = plr.PlayerGui
script.Parent:SetAttribute("CurrentUi","InteractUI")
end
else
if ui then
script.Parent:SetAttribute("CurrentUi","")
ui:Destroy()
end
end
end
end
end

7 comments
I updated the script
There is maybe some latency. Why don't you just enable/unenable the Gui on client for 0 latency?
When I did this it errored saying: attempt to index nil with 'InteractFrame'
Why not use a proximity prompt?
I considered using them. I decided not to so I can customise the Gui more
You can use custom UIs in proximity prompts.
I've fixed it now, thanks anyway!