Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How should I define the player in a server script?

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

0

I updated the script

1

There is maybe some latency. Why don't you just enable/unenable the Gui on client for 0 latency?

0

When I did this it errored saying: attempt to index nil with 'InteractFrame'

0

Why not use a proximity prompt?

0

I considered using them. I decided not to so I can customise the Gui more

0

You can use custom UIs in proximity prompts.

0

I've fixed it now, thanks anyway!

2 answers

This is easy so we use something called and for i,v in pairs loop

Here's the code

for i,v in pairs(game.Players:GetPlayers()) do
	-- Write the code you want to do to the player for example v means the local player
	v.Character:MoveTo(Vector3.new(The position for your character))
end)

4 comments

0

I dont think you get what I'm going for, I dont want to move the character, I want to detect when the player is close to a part.

0

I did move for an example

0

I have already used a for loop

0

ok

This discussion is preserved as it was. New answers, comments, and votes are closed.