I want my script to when where I touch the part the player see’s a text that appears on the screen. My OnTouch event script is not working. Can you help me? This is my code, this is a script inside the part I want to be touched for the text to apepar.
local Text = game.StarterGui.TextAppear.Frame.TextAppear1
local Part = script.Parent
local function onTouch(otherPart)
local humanoid = otherPart.Parent:FindFirstChild('Humanoid')
if humanoid then
Text.Parent.Visible = true
wait(2.5)
Text.Parent.Visible = false
end
end
Part.Touched:Connect(onTouch)
Editing objects inside StarterGui does not actively replicate to players in the game. This means when you update a property of an object inside StarterGui, it will not be reflected in players’ GUI. You must use player.PlayerGui in place of StarterGui in this case.
For example, you could say:
local Players = game:GetService("Players")
local part = script.Parent
local function onTouch(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
local player = Players:GetPlayerFromCharacter(hit.Parent)
if player and humanoid then
local text = player.PlayerGui.TextAppear.Frame.TextAppear1
text.Parent.Visible = true
wait(2.5)
text.Parent.Visible = false
end
end
part.Touched:Connect(onTouch)
-- NOTE: The code above will only show the GUI to the player that touched the part. Read below if it should show to everyone.
If you wanted the GUI to show up for all users in the server, you’d have to loop through the players using a for loop and edit the GUI in their PlayerGui individually.
Alternatively (and probably easier) you could use a RemoteEvent, where you detect the touch on the server then fire the remote event to all players. If you would like the GUI to show for all players at the same time I’d recommend this option, as it involves keeping the handling of the UI on the client. If you would like code to explain how to do this I can provide.