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

How can I make a gui appear when a block is touched?

So I am struggling while doing this little project I am working on, basically I am trying to make a GUI that pops up onto the screen when a local player touches it. If more info is needed let me Know!

local gui = game.ReplicatedStorage.ScreenGui

function onTouched(hit)
	local humanoid = hit.parent
	if humanoid~=nil then
		local clone = gui:Clone()
		clone.Parent = game.Players.LocalPlayer.PlayerGui
	end
end
script.Parent.Touched:Connect(onTouched)

5 comments

0

This isn't a request SITE

0

Scriptinghelpers is NOT a script request site. https://scriptinghelpers.org/help/community-guidelines

0

@14zanderbilt and @Void_Frost : You do not understand what a request is. A request is when someone does NOT attempt to do something. This asker actually tried by putting the gui into the LocalPlayer's PlayerGui (which won't work).

0

@youtubemasterWOW We DO understand what a request is, you know people can edit questions, right? That's what he did. Originally, there was no code, and it was just a request.

0

Okay. Sorry though. I though that was his original question.

1 answer

✓ Accepted answer

Hello.


Problem:

  • You used LocalPlayer in a ServerScript. This wouldn't work as ServerScripts do not run on a client.

Solution:

  • Use the GetPlayerFromCharacter() function. It will get the player from its character, hit.Parent.

Recommendations:

  • Use an uppercase "P" for "parent".

  • You do not need the ~= nil as the if statement wil automatically check if it's not nil.


Fixed Script:

local gui = game.ReplicatedStorage.ScreenGui

local function onTouched(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)

    if player then
        local clone = gui:Clone()
        clone.Parent = player.PlayerGui
    end
end

script.Parent.Touched:Connect(onTouched)

2 comments

0

Thank You @YoutubeMasterWOW ! now all i need to do is add a wait and then :Destroy() !

0

No problem.

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