Touching part doesn't turn off the core GUI

I’m trying to turn off the main core GUI by touching a part.
I have a part inside the workspace with the local script inside.

It doesn’t turn off the core gui.

I’ve looked through a few dev forum posts and couldn’t find a solution.

local StarterGui = game:GetService("StarterGui")
local Players = game:GetService("Players")
local part = script.Parent

local function onPartTouch(part)
	local player = Players:GetPlayerFromCharacter(part.Parent)
	if player then
		onPartTouch:SetCoreGuiEnabled(Enum.CoreGuiType.All, false)
	end
end

part.Touched:Connect(onPartTouch)

Brick

1 Like

I’d bet it’s because you had tried to set the core gui from a function name instead of StarterGui

StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, false)

What? Remove onPartTouch
try this

game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, false)

Localscripts don’t work in Workspace. You would need to perform a serverside firing to the client using Event:FireClient() or running the script in another local module such as StarterCharacter/PlayerScripts.

Also make it StarterGui:SetCoreGuiEnabled, not onPartTouch

1 Like

Going with what MasterTiitus has said, you could change the script inside the part to a script, and add a local script to the player. (I would put inside StarterGui)

Part Script

local function getPlayerFromCharacter(character)
	for _, player in pairs(game:GetService("Players"):GetPlayers()) do
		if player.Character == character then
			return player
		end
	end
end

script.Parent.Touched:Connect(function(hit)
	local player = getPlayerFromCharacter(hit.Parent)
	if player then
		player.PlayerGui["Your localscript name"].RemoteEvent:FireClient(player)
	end
end)

Local Script

local StarterGui = game:GetService("StarterGui")

script.RemoteEvent.OnClientEvent:Connect(function()
	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, false)
end)

Just a rough script and easily changeable

1 Like

No, run remotes in ReplicatedStorage not StarterGui. Basic script in StarterPlayer. What even is your GetPlayerFromCharacter, just use robloxs own.

Also it won’t work as you returned a character instead of the actual Player:FindFirstChild.

True let me edit my comment real quick

Create a remotevent called CoreEvent in ReplicatedStorage.
– ServerScript inside Part

local db = false
script.Parent.Touched:Connect(function(hit)
	local player = game:GetService("Players"):FindFirstChild(hit.Parent)
	if db == true then return end
	if player then
		game:GetService("ReplicatedStorage").CoreEvent:FireClient(player)
        db = true
        task.wait(3)
        db = false
	end
end)

– LocalScript inside StarterPlayerScripts

local StarterGui = game:GetService("StarterGui")
game:GetService("ReplicatedStorage").CoreEvent.OnClientEvent:Connect(function()
    if player then
	   StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, false)
    end
end)
1 Like

Whu not just use game:GetService("Players"):GetPlayerFromCharacter()?

You can use that, I really don’t know why I used a local function.