I have been trying to make a system where if the player touches a block it will fire an event containing the players name, which would then change the text of a textlabel to the player’s name. However, this is not happening and I’m getting no errors so I am a bit confused.
First script (localscript inside of the part)
local RS = game.ReplicatedStorage
local Event = RS.PlayerFinished
local Players = game:GetService("Players")
script.Parent.Touched:Connect(function(toucher)
if toucher.Parent:FindFirstChild("Humanoid") then
local player = Players:GetPlayerFromCharacter(toucher.Parent)
if player then
local name = player.Name
Event:FireServer(name)
end
end
end)
Second Script (server script inside of the text label):
local RS = game.ReplicatedStorage
local Event = RS.PlayerFinished
function onPlayerFinished(name)
script.Parent.Visible = true
script.Parent.Text = name .. " has finished!"
end
Event.OnServerEvent:Connect(onPlayerFinished)
the remote event to the server always sends player.
This means
local RS = game.ReplicatedStorage
local Event = RS.PlayerFinished
function onPlayerFinished(player,name)
script.Parent.Visible = true
script.Parent.Text = name .. " has finished!"
end
Event.OnServerEvent:Connect(onPlayerFinished)
I added that part, then I also added a print statement (because I forgot to do that the last time) to see if the event was even being fired.
local RS = game.ReplicatedStorage
local Event = RS.PlayerFinished
function onPlayerFinished(player,name)
print("Works")
script.Parent.Visible = true
script.Parent.Text = name .. " has finished!"
end
Event.OnServerEvent:Connect(onPlayerFinished)
It doesn’t print “Works”, so I’m guessing there’s something wrong with the localscript
That is because in the onPlayerFinished function you only have one thing in the Connect(function(here)), you would need to add another thing, like this
function onPlayerFinished(player,name)
The first parameter is the player who fired the server, which is a string, not text, the second one is any other information you sent while firing the event, in this case it should be name, if name doesn’t work just do player.Name as the replacement for name in the script for changing the textlabel
Edit:I just realized @kalabgs did the same thing, add prints in the local script to see where the error happens
local RS = game.ReplicatedStorage
local Event = RS.PlayerFinished
local Players = game:GetService("Players")
script.Parent.Touched:Connect(function(toucher)
print("Work 1")
if toucher.Parent:FindFirstChild("Humanoid") then
print("Work 2")
local player = Players:GetPlayerFromCharacter(toucher.Parent)
if player then
print("Work 3")
local name = player.Name
Event:FireServer(name)
print("Work 4")
end
end
end)
the first print doesn’t print, I don’t know why it’s not recognizing it being touched
I think it is because it is in a local script, maybe if you put that code in a regular script and just do the textlabel stuff in that script, no firing the server though