Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

My gui will not show when players touch the wall?

Asked by 5 years ago

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

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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!

0
Can't you just use the .Touched event in a single localscript? ihatecars100 502 — 5y
0
no, they dont run in workspace Gey4Jesus69 2705 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
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)

Answer this question