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

My gui will not show when players touch the wall?

I made a gate, with a invisible wall, so players cannot leave, when they touch it text appears. But it will not show.

local leave = script.Parent
local player = game.Players.LocalPlayer
local Dialouge2 = player.PlayerGui.ScreenGui
local function YouTried(leave)
	local parent = leave.Parent
	if game.Players:GetPlayerFromCharacter(parent) then
		 Dialouge2.Enabled = true
		wait(2)
	Dialouge2.Enabled = false
	end
	
end
leave.Touched:Connect(YouTried)

2 answers

The issue here is that you're attempting to run a local script in workspace. Local scripts can only run in places which reference the local player (i.e. Backpack, StarterGui, or any place that is a descendant of the player object at some point). To fix this, we'll use a server script to handle the Touched event and fire a remote event to the client, where the Gui will be enabled.

--Server script in part

local leave = script.Parent
local remote = Instance.new("RemoteEvent")
remote.Parent = game:GetService("ReplicatedStorage")

leave.Touched:Connect(function(hit)
   local player = game.Players:GetPlayerFromCharacter(hit.Parent)
   if player then
      remote:FireClient(player,"") --you can send any data through the second argument
   end
end)

--Local script in acceptable place, such as the ones listed above

local player = game:GetService("Players").LocalPlayer
local remote = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")

remote.OnClientEvent:Connect(function(args) --the data you sent
   local Dialouge2 = player.PlayerGui:FindFirstChild("ScreenGui")
   if Dialouge2 then --ensure existence
      Dialouge2.Enabled = true
      wait(2)
      Dialouge2.Enabled = false
   end
end)

Resources:

Remote Events

Acceptable Places for Local Scripts (below first paragraph)

Accept and upvote if this helped!

2 comments

0

Can't you just use the .Touched event in a single localscript?

0

no, they dont run in workspace

local leave = script.Parent
local player = game.Players.LocalPlayer
local Dialouge2 = player.PlayerGui.ScreenGui

leave.Touched:Connect(function(hit)
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		Dialouge2.Enabled = true
		wait(2)
		Dialouge2.Enabled = false
	end
end)

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