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

How can I make a script where where a player gets close to an object a Gui Pops up?

This is what I mean, http://wiki.roblox.com/index.php?title=Libraries_and_Samples/ImageInputLibrary

And Heres my Code, I feel like this could structured in a better way.


function GetPlayer(Position)
	local Child = workspace:GetChildren()
	local Distance = 10
	for i=1 , #Child do
		if Child[i].ClassName == "Model" then
			local Humanoid = Child[i]:FindFirstChild("Humanoid")
			if Humanoid ~= nil then
				local CheckTorso = Child[i]:FindFirstChild("Torso")
				if CheckTorso ~= nil then
					if (CheckTorso.Position - Position).Magnitude < Distance then
						SayHi()
					end
				end
			end
		end
	end
end

function SayHi()
	print("Hi")
	wait(5)
end

game:GetService("RunService").RenderStepped:connect(function()
	GetPlayer(script.Parent.Position)
end)

1 answer

Just a thing I want to tell you, you don't have to search the workspace for the character, every player has a character, which you can acces using player.Character, so instead of looping though the workspace try this

for i = 1, #game.Players:GetPlayers() do
	local character = game.Players[i].Character
end

And instead of:

if CheckTorso ~= nil then
	-- Stuff
end

you could do:

if CheckTorso then
	-- Stuff
end

Note: These where just some tricks that can save some time.

______________________________________________________________________________

I'm not really sure about this But, it may be cause the function was called before you made the function. Anyway, here is the script I came with

function SayHi()
    print("Hi")
    wait(5)
end

function GetPlayer(Position)
	local Distance = 10
	for _, player in pairs(game.Players:GetPlayers()) do
		if player.Character:FindFirstChild("Humanoid") then
			if (player.Character.Torso.Position - Position).Magnitude < Distance then
				SayHi()
			end
		end
	end
end

game:GetService("RunService").RenderStepped:connect(function()
    GetPlayer(script.Parent.Position)
end)

Sorry if I didn't help, this is probally my worst answer I made :/

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