So I have been working on a game and in the game, I wanted to make it so when someone in the game touches a block it will display that person’s name on a GUI, with some other text ofc.
Example being -
Tom352 touches the block
“Tom352 - Found it”
I have a little script and if you want to adapt to it here it is
local brick = script.Parent
local debounce = true
local function doSomething(otherPart)
if debounce and otherPart.Parent:FindFirstChildWhichIsA(“Humanoid”) then
debounce = false
print(“Hello”)
wait(2)
debounce = true
end
end
local connection = brick.Touched:Connect(doSomething)
You don’t need to Connect your events like that, I don’t know why you’d wanna do it like that
You could just simply detect using the GetPlayerFromCharacter function
local brick = script.Parent
local debounce = true
local GUI = workspace.Part.BillboardGui
local function doSomething(otherPart)
local Player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
if debounce and Player then
debounce = false
GUI.TextLabel.Text = Player.Name
print("Hello")
wait(2)
debounce = true
end
end
end
brick.Touched:Connect(doSomething)
@GamingExpert0312 You’re only referencing the BodyPart that the Touched event detected, so that would instead result as a nil value
Sorry I’m a little new to this but I have Part with the script you sent in it and I have a GUI with a frame and a TextLabel under StarterGui but when I run the game and step on the part I get this error
GetPlayerFromCharcter is not a valid member of Players “Players”
I changed the local GUI in the script you sent to local GUI = game.StarterGui.Gui.Text.TextLabel
I might be overcomplicating things but i am a little new so bear with me
Also don’t use the StarterGui in a Server Script, that’ll only be replicated to the server
But instead, change it to a LocalScript and parent your brick variable to where you want to touch your brick:
local Player = game.Players.LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")
local brick = --Your brick here
local debounce = true
local function doSomething(otherPart)
local Target = game.Players:GetPlayerFromCharacter(otherPart.Parent)
if debounce and Target == Player then
debounce = false
PlayerGui.Gui.Text.TextLabel.Text = Player.Name
print("Hello")
wait(2)
debounce = true
end
end
end
brick.Touched:Connect(doSomething)
wait I just have one more question is there any way I can make it so when someone touches the part it will display the text to everyone on the server, not just myself.
Well, you’d need to use a RemoteEvent for that which can handle server-client transportation
I don’t think it’d be too hard, but you would need to reference the Touched Event as a server script that’s parented in your Part again:
local brick = script.Parent
local debounce = true
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local function doSomething(otherPart)
local Player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
if debounce and Player then
debounce = false
Event:FireAllClients(Player.Name)
print("Hello")
wait(2)
debounce = true
end
end
end
brick.Touched:Connect(doSomething)
And then on your LocalScript that will change the GUI that should be parented inside the StarterGui:
local Player = game.Players.LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
Event.OnClientEvent:Connect(function(PlayerPartName)
PlayerGui.Gui.Text.TextLabel.Text = PlayerPartName
end)