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.

text = Instance.new("TextLabel")
screen = Instance.new("ScreenGui")
script.Parent.Touched:Connect(function(hit)
    if hit.Parent:IsA("Player") then
        screen.Parent = game.StarterGui
        text.Parent = game.StarterGui:WaitForChild("ScreenGui")
        text.Text = "Welcome"
    end 
end)
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.

local NewGui = Instance.new("RemoteEvent") --Create the remote
NewGui.Name = "NewGui" --Name the remote
NewGui.Parent = game.ReplicatedStorage --Put the remote in ReplicatedStorage

script.Parent.Touched:Connect(function(Hit) --When script.Parent is touched, define 'Hit' as what touched it
    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
    if Player ~= nil then --If it was really a player that touched it, then
        NewGui:FireClient(Player) --Fire the remote on the client
    end
end)

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

game.ReplicatedStorage:WaitForChild("NewGui").OnClientEvent:connect(function() --When NewGui is triggered by the server script on the player
    local ScreenGui = Instance.new("ScreenGui") --Create the ScreenGui
    local TextLabel = Instance.new("TextLabel") --Create the TextLabel
    ScreenGui.Parent = script.Parent --Put the ScreenGui in PlayerGui
    TextLabel.Parent = ScreenGui --Put the TextLabel in ScreenGui
    TextLabel.Text = "Welcome" --Change the TextLabel's Text
end)
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


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

this goes in a script in server script service

game.ReplicatedStorage.OpenGui.Event:Connect(function(plrName)
text = Instance.new("TextLabel")
    screen = Instance.new("ScreenGui")
local plr = game.Players[plrName]
 screen.Parent = plr.PlayerGui
            text.Parent = Plr.PlayerGui:WaitForChild("ScreenGui")
            text.Text = "Welcome"
end)

Answer this question