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

Creating a block that when touched, inserts a script into their StarterGui? [SOLVED]

I'm not requesting a full script, I'm just at loss for how to go about this. As of now, this is what I'm pretty sure I know I'll have to do:

  • Place the script into lighting
  • Make sure the script is local, and the coding is compatible with such
  • I might, in some way, have to use "Clone()"? (I'm not familiar with its parameters, or its function)

However, these are the issues I'm encountering:

  • How do I remotely insert something from lighting into a player by name of object?
  • (The big one) How do I know which player to insert the script into? (For this script, it does matter)

Any help possible is greatly appreciated, since this is a bit out of my skill zone, and is one of the last things I have left to code in my newest project.

Thanks for reading!

2 answers

✓ Accepted answer

To fix the biggest problem, you would need to detect if a player actually touches the brick. Like so:

local brick = Workspace.Part
function onTouch(part)
	if part.Parent:FindFirstChild("Humanoid") ~= nil then
	--Code
	end
end
brick.Touched:connect(onTouch)

This detects a humanoid in the parent model of part using FindFirstChild(). It should not equal nil if a player touched as the part would equal some body part, and the parent of that should have a humanoid in it.

Now, to find the player's gui screen, make a clone of the script, then move it to the GUI, we would use GetPlayerFromCharacter and yes, the :Clone(). Like so:

local brick = Workspace.Part
function onTouch(part)
	if part.Parent:FindFirstChild("Humanoid") ~= nil then
		local player = game.Players:GetPlayerFromCharacter(part.Parent)
		local clone = game.Lighting.Script:Clone() --Replace script with the name of your script
clone.Parent = player.PlayerGui.ScreenGui --Setting the parent. Change ScreenGui to whatever the name of your GUI is.
	end
end
brick.Touched:connect(onTouch)

To make sure it isn't spammed, you would use a debounce value too.

local debounce = false
local brick = Workspace.Part
function onTouch(part)
	if part.Parent:FindFirstChild("Humanoid") ~= nil and debounce == false then
		debounce == true
		local player = game.Players:GetPlayerFromCharacter(part.Parent)
		local clone = game.Lighting.Script:Clone() --Replace script with the name of your script
clone.Parent = player.PlayerGui.ScreenGui --Setting the parent. Change ScreenGui to whatever the name of your GUI is.
		wait(1)
		debounce = false
	end
end
brick.Touched:connect(onTouch)

2 comments

0

Nice name. I like Pokemon.

1

Wow, very detailed answer! I just learned quite a bit, even about some things not pertaining to the question. However, the method is working perfectly in the full script I needed it in. Thank you!

Brick.Touched:connect(function(part) -- This is the event. You seem to be inexperienced with 	  functions, so I'll walk you through it.
	if game.Players:GetPlayerFromCharacter(part.Parent) then -- This is checking to see if the part belongs to a player's character
		player = game.Players:GetPlayerFromCharacter(part.Parent)
		gui = game.Lighting.GuiHere:Clone() -- This clones the GUI you want to insert.
		gui.Parent = player.PlayerGui -- This makes the GUI visible to the player. (Note: GUI must refer to a StarterGui.)
	end
end)

1 comment

0

Thank you for the help! My main issue was using variables when referencing a location, since it usually comes up with an error. Between your answer and Octillerysnacker's, I think I now know what I'm doing!

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