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

Touched event Problems with Instance.new any help?

Asked by 6 years ago

im tryna make a script where if u step on a part it creates a screengui and then creates a text label.

1text = Instance.new("TextLabel")
2screen = Instance.new("ScreenGui")
3script.Parent.Touched:Connect(function(hit)
4    if hit.Parent:IsA("Player") then
5        screen.Parent = game.StarterGui
6        text.Parent = game.StarterGui:WaitForChild("ScreenGui")
7        text.Text = "Welcome"
8    end
9end)
0
is it filtering ebnabled? mattchew1010 396 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

your entire script is wrong if your game is not in experimental mode, then you need to use a RemoteEvent, that should be parented to game.ReplicatedStorage, but I assume you don't know how to do that so I comment every line in the following script with detailed explanations

Server Script (For creating the remote, and handling the Touched event) The Server Script should be inside the Part.

01local NewGui = Instance.new("RemoteEvent") --Create the remote
02NewGui.Name = "NewGui" --Name the remote
03NewGui.Parent = game.ReplicatedStorage --Put the remote in ReplicatedStorage
04 
05script.Parent.Touched:Connect(function(Hit) --When script.Parent is touched, define 'Hit' as what touched it
06    local Player = game.Players:GetPlayerFromCharacter(Hit.Parent) --Attempt to get the player who touched it, by using the GetPlayerFromCharacter method that gets a player by his character
07    if Player ~= nil then --If it was really a player that touched it, then
08        NewGui:FireClient(Player) --Fire the remote on the client
09    end
10end)

Local Script (For handling the Remote, and creating the Gui) The Local Script should be inside StarterGui.

1game.ReplicatedStorage:WaitForChild("NewGui").OnClientEvent:connect(function() --When NewGui is triggered by the server script on the player
2    local ScreenGui = Instance.new("ScreenGui") --Create the ScreenGui
3    local TextLabel = Instance.new("TextLabel") --Create the TextLabel
4    ScreenGui.Parent = script.Parent --Put the ScreenGui in PlayerGui
5    TextLabel.Parent = ScreenGui --Put the TextLabel in ScreenGui
6    TextLabel.Text = "Welcome" --Change the TextLabel's Text
7end)
Ad
Log in to vote
-2
Answered by 6 years ago

if filtering enabled make a bindable event called openui in replicated storage this script goes in the part

1script.Parent.Touched:Connect(function(hit)
2    if hit.Parent:IsA("Player") then
3    game.ReplicatedStorage.OpenGui:Fire(Hit.Parent.Name)
4    end
5end)

this goes in a script in server script service

1game.ReplicatedStorage.OpenGui.Event:Connect(function(plrName)
2text = Instance.new("TextLabel")
3    screen = Instance.new("ScreenGui")
4local plr = game.Players[plrName]
5 screen.Parent = plr.PlayerGui
6            text.Parent = Plr.PlayerGui:WaitForChild("ScreenGui")
7            text.Text = "Welcome"
8end)

Answer this question