Help with my script

Hello developers! I am currently trying to make an elimination game, and when you step into a place, AKA, touch a massive invisible, CanCollide off, part, in which inside of it, a local script is placed…

I want it so when you touch the part, the GUI is shown, saying, “You’re in!”

Here is the script below! :arrow_down:

local inpart = script.Parent
local plr = game.Players.LocalPlayer
local text = script.Parent.Parent.intext
text.Visible = false

local function PopupReady(inpart)
text.Visible = true
end

local function PopupGone(inpart)
	text.visible = false
end

inpart.Touched:Connect(PopupReady(plr))

inpart.TouchEnded:Connect(PopupGone(plr))

Currently, the gui pop up is constantly shown, even when you are not ‘inside’ the part.
Help would be good! Thank you in advance! :smiley:

1 Like

A LocalScript inside a part? You should put it in StarterGui or so.

1 Like

The use of region 3 would be better. If you want to use Region3 the LocalScript has to be in StarterPlayerScript.

If you don’t know how to use it this is a good tutorial for beginners Roblox - Play music in different areas - Scripting tutorial (2020 updated version) - YouTube, on the video it’s used to play sounds but you can easelg change it with show/hide the gui

2 Likes

I moved it to screenGui, it stayed the same!

Why isn’t the way I’m doing it working though?

Because when the player touches the part both functions fires and so it keeps on the screen

but I made it so that when the touch ends, visible = false. so it should = false

I am getting more and more confused…
As soon as I press play, it is on the screen, I haven’t touched the part or anything…

No you didn’t as I can see from the script, if you really want to use this method that I don’t suggest to use you will need to add a denounce that gets set to true for an amount of time you want when the part is touched and another one that is set to true when the function that fires when the player is in and to false with the other function, however this is not a good method to detect when the player is in and you should use Region3.

That happens because the function fires even if it’s not a player to touch it to fix it you have to add a check to control if the part is touched by a player for example checking if the part that touches it contain an humanoid

Hi There!
You can simply fix this by putting the script in StarterGui/StarterPlayerScripts and removing the (plr) argument where you call the function
like this -

inpart.Touched:Connect(PopupReady)

inpart.TouchEnded:Connect(PopupGone) 

And replace v with capital V of the visible in the 12th line (remember, case sensitive)

That is what the

local plr = game.Players.LocalPlayer

does, and also the

local function PopupReady(inpart)
	text.Visible = true
end

inpart.Touched:Connect(PopupReady(plr))

checks for a plr (game.Players.LocalPlayer) who touches it

Hey there! :wave:

An issue I would assume could occur is that .Touched either doesnt register OR it will simply fire multiple times over and over again. (same with .TouchEnded)

I would suggest you use Region3s to accomplish what you are looking for!

do I need to change it to

local inpart = workspace.inpart

???

No it doesn’t do what you think, you are actually giving to the function a parameter thst it never uses

Probably yes, you have to tell the script where the part and the Gui is.

Also, you don’t have to write “inpart” as a parameter of the function, if you want to make sure the part is touched by a player then add a code that will run the function when a player’s character is touching the part…
Like this -

    local Players = game:GetService("Players")

    local function PopupReady(hit)
    	local player = Players:GetPlayerFromCharacter(hit.Parent)
        if player ~= Players.LocalPlayer then
		return
	end
    	
    		text.Visible = true
    		
    end

would this be correct:

local inpart = workspace.inpart
local plr = game.Players.LocalPlayer
local text = game.StarterGui.ScreenGui.intext

local Players = game:GetService("Players")

local function PopupReady(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		text.Visible = true
	end
end


inpart.Touched:Connect(PopupReady)

I didn’t do PopupGone on that script…

Yep, this will work, try it by completing the PopupGone function.

try to add a variable that acts like a debounce like
local PartTouched = false and set it where if the PartTouched = false when the player touches it then you can make the text visible you can do the same for the touch ended event. so your script would kinda look like this

local inpart = script.Parent
local plr = game.Players.LocalPlayer
local text = script.Parent.Parent.intext
local PartTouched = false
text.Visible = false

local function PopupReady(inpart)
if PartTouched == false then
        text.Visible = true
    end
end

local function PopupGone(inpart)
    if InPart == true then
	   text.visible = false
    end
end

inpart.Touched:Connect(PopupReady(plr))

inpart.TouchEnded:Connect(PopupGone(plr))

Hey! I have got this script that doesn’t work!

 local inpart = script.Parent
local plr = game.Players.LocalPlayer
local text = script.Parent.Parent.intext
local PartTouched = false
text.Visible = false

local inpart = workspace.inpart
local plr = game.Players.LocalPlayer
local text = game.StarterGui.ScreenGui.intext

local Players = game:GetService("Players")

local function PopupReady(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		text.Visible = true
		print("Stepped in!")
	end
end

local function PopupGone(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		print("Stepped out!")
		text.Visible = false
	end
end


inpart.Touched:Connect(PopupReady)

inpart.Touched:Connect(PopupReady) 

This is where the parts are positioned:


any help with this?

you need something like
local player = game.Players.LocalPlayer
in the touch connections
if player == plr then
(your stuff)
also u need the params for popupgone and the other one as (plr, part)
and u need if part.Parent:FindFirstChild(“Humanoid”)