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

Anyone know how to make touch brick remove GUIs from the player?

Hey there, trying to make a script that will remove everything from PlayerGui from Player on the event of touching a brick, but have ran into some issues. Could anybody help me out? not the best scripter so any advice would help tons!

local brick = script.Parent
local function steppedOn(part)
	local parent = part.Parent
	if game.Players:GetPlayerFromCharacter(parent) then
		game.Players.LocalPlayer.PlayerGui:Destroy()
end
end
brick.Touched:connect(steppedOn)

2 answers

✓ Accepted answer

Problem: Guis are not getting removed.

Solution: Use game.Players:GetPlayerFromCharacter(parent) instead of game.Players.LocalPlayer.

Explanation: game.Players.LocalPlayer does not work in ServerScripts as ServerScripts do not run on clients. Instead, they run on a physical server. game.Players:GetPlayerFromCharacter(parent) is equal to the player who touched the part.

Other Improvements: Use DataModel:GetService("Players") and RBXScriptSignal:Connect(steppedOn) instead of DataModel.Players and RBXScriptSignal:connect(steppedOn).

Fixed Code:

local brick = script.Parent

local function steppedOn(part)
	local player = game:GetService("Players"):GetPlayerFromCharacter(part.Parent)

	if player then
		for i, v in pairs(player.PlayerGui:GetChildren()) do
       			v:Destroy()
		end
	end
end

brick.Touched:Connect(steppedOn)

Please upvote and accept this answer if it helped.

Try This:

local brick = script.Parent
local function steppedOn(part)
    local parent = part.Parent
    if game.Players:GetPlayerFromCharacter(parent) then
        game.Players.LocalPlayer.PlayerGui:GetChildren()Destroy()
end
end
brick.Touched:connect(steppedOn)

1 comment

0

add : between getchildren and destroy

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