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

how do i run touched event on local script?

so im making an easter egg in my game and there is a part that changes text label text But only for the player who touched the brick. Yes the script is in starter gui.

local part = game.Workspace.special.evenmorespecial.secret
part.Touched:Connect(function()
	local text1 = "..."
	local text2 = "why would someone like this come here?"
	local text3 = "hmm?"
	local text4 = "i guess you dont like to talk."
	local text5 = "... or you are a bit shocked?"
	local text6 = "you probably got here by accident... just like i did..."
	local text7 = "well, i didn't see a single human for years!"
	local text8 = "i'll reward you with the ability to spawn the green dirt."
	local text9 = "bye!"
	local text = game.StarterGui.TFlaniganDialogue.d1
	wait(1)
	for i = 1, #text1 do
	    text.Text = string.sub(text1, 1, i)
	    wait(0.04) 
	end
	wait(3)
	for i = 1, #text2 do
	    text.Text = string.sub(text2, 1, i)
	    wait(0.04) 
	end
	wait(4)
	for i = 1, #text3 do
	    text.Text = string.sub(text3, 1, i)
	    wait(0.04)
	end
	wait(3)
	for i = 1, #text4 do
	    text.Text = string.sub(text4, 1, i)
	    wait(0.04)
	end
	wait(4)
	for i = 1, #text5 do
	    text.Text = string.sub(text5, 1, i)
	    wait(0.04) 
	end
	wait(3)
	for i = 1, #text6 do
	    text.Text = string.sub(text6, 1, i)
	    wait(0.04) 
	end
	wait(3)
	for i = 1, #text7 do
	    text.Text = string.sub(text7, 1, i)
	    wait(0.04) 
	end
	wait(3)
	for i = 1, #text8 do
	    text.Text = string.sub(text8, 1, i)
	    wait(0.04) 
	end
	wait(4)
	for i = 1, #text9 do
	    text.Text = string.sub(text9, 1, i)
	    wait(0.04) 
	end
end)

2 answers

✓ Accepted answer

Well you can put the touched event in a serverscript and use a remote event to fire a signal to the client, then use OnClientEvent to handle the request.

Examples:

-- Server Script
part.Touched:Connect(function(hit)
        if hit.Parent:FindFirstChild("Humanoid") then
                local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
                remoteEvent:FireClient(plr)
        end   
end)
-- Client script in startergui
remoteEvent.OnClientEvent:Connect(function()
-- your easter egg code here
end)

3 comments

0

it says onclientevent can be only used on the client. do i have to do something with local scripts?

0

For server->client, use remoteEvent.OnClientEvent in a localscript(s) and use remoteEvent:FireClient(player) or remoteEvent:FireAllClients() in a server script. Client->Server should instead use remoteEvent:FireServer() in a localscript and use remoteEvent.OnServerEvent in server script

On the same local script do

Local variable = what the local part's name is

If variable.touched:connect(function ()
       Print("or code goes here")
end)

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