(SOLVED) Need help with making a Script

Hey, So basically, I’m trying to make a script that allows this

Once a player touches a Part (TestPart), it will disable a ScreenGui (TestGui),

This is how i have it placed

This is the script

local part = workspace.TestPart 
local screenGuiName = "TestGui"

local function onTouch(otherPart)
	local character = otherPart.Parent
	local humanoid = character:FindFirstChildWhichIsA("Humanoid")

	if humanoid then
		local player = game.Players:GetPlayerFromCharacter(character)
		local playerGui = player:FindFirstChild("PlayerGui")
		local screenGui = playerGui:FindFirstChild(screenGuiName)

		if screenGui then
			screenGui.Enabled = false
		end
	end
end

part.Touched:Connect(onTouch)

It won’t disable the UI once the player has touched the part. How can i make this work?

Thank you!

Local scripts do not run if it is a descendant of workspace. Try placing the script in the ScreenGui.

Also, it is a local script meaning it can’t affect other people except the client running it. I would first set the player variable to game.Players.LocalPlayer and make it so only if the player’s character touches it, then disable the GUI.

1 Like

Close but not technically true, with normal scripts you can set the RunContext to Client and then it will run in the workspace.

1 Like

That’s true but in this case, it is a local script.

2 Likes

The first Solution you gave worked!

I tested it with 2 players, and only 1 player gets it disabled! Just what i wanted.

And I’ve been reading the paragraph you wrote,
What would replacing the player variable do?

The script already works the way it is, so I wouldn’t mind it for now.

But if you were to do local player = game.Players.LocalPlayer, it would return the player that is currently running the local script.

2 Likes

Ah okay! Thanks for replying! Have a great day!

2 Likes

Hey just a question, I rewrote the code slightly.

Could i do something like this:

local parts = {
	workspace.TestPart1,
	workspace.TestPart2,
	workspace.TestPart3,
}

local screenGuiName = "TestGui"

local function onTouch(otherPart)
	local character = otherPart.Parent
	local humanoid = character:FindFirstChildWhichIsA("Humanoid")

	if humanoid then
		local player = game.Players:GetPlayerFromCharacter(character)
		local playerGui = player:FindFirstChild("PlayerGui")
		local screenGui = playerGui:FindFirstChild(screenGuiName)

		if screenGui then
			screenGui.Enabled = false
		end
	end
end

for _, part in ipairs(parts) do
	part.Touched:Connect(onTouch)
end

I rewrote it, So i could add more “TestPart”(s)

Would this work if (For example) i made 100 TestParts. Or is there an easier way i could make it, So it reads Every single part with the name “TestPart”?

Thanks

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.