I have timer (text label)
I need to send an event from client script to server script with timer’s text, when part is touched
Local script, that sends an event when part is touched:
local player = script.Parent.Parent.Parent
workspace.TestPart.Touched:Connect(function(touch)
if touch.Parent:FindFirstChildOfClass("Humanoid") then
local timerText = script.Parent.TimerLabel.Text
game.ReplicatedStorage.TestEvent:FireServer(player, timerText)
end
end)
while wait() do
if player.TimerVisible.Value == true then
script.Parent.Enabled = true
script.Parent.TimerLabel.TimerScript.Disabled = false
else
script.Parent.TimerLabel.TimerScript.Disabled = true
script.Parent.Enabled = false
end
end
Server script:
game.ReplicatedStorage.TestEvent.OnServerEvent:Connect(function(player, text)
local timerText = text
print(timerText)
end)
Result (Printed Text):
What I tried to do?
I tried to remove touched event but result is same
Also I tried to add this to line when event is sending to server:
The client doesn’t need to define itself to the server as the first variable is the player by default. Therefore, in the following script:
game.ReplicatedStorage.TestEvent.OnServerEvent:Connect(function(player, text)
local timerText = text
print(timerText)
end)
The first and second variable are both your player.
However, the more concerning part is that you’re using a loop to check if something has changed. I would recommend using the Changed Event or GetPropertyChangedSignal Event.
On top of that, you script doesn’t check who touched the part. If there were multiple players in the game , each player would fire the server every time it was touched. On this line:
if touch.Parent:FindFirstChildOfClass("Humanoid") then
I would add some code to check that it is the correct player:
if touch.Parent:FindFirstChildOfClass("Humanoid") and touch.Parent.Name == player.Name then