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

Would i rather use a OnServerEvent or OnClientEven for this?

I have a game where the data value's are transferred to another game. So my question is that when a player joins and if one of their values is 1, how would i clone something towards the player? Should i use :

LocalScript:

local Hair1 = game:GetService("ReplicatedStorage").Events:FindFirstChild("Hair1")

game.Players.PlayerAdded:Connect(function(player)
repeat until player.Data:wait()

if player.Data.Hair.Value == 1 then
Hair1:FireServer
end
end)

Server Script:

local Hair1 = game:GetService("ReplicatedStorage").Events:FindFirstChild("Hair1")

Hair1.OnServerEvent:Connect(function(player)
--clone's hair to player
end)

1 answer

Yes. You should use OnServerEvent when a client fired a remote event (i'm assuming it is) and the server is listening for it. However, you should use OnClientEvent when the server fired a remote event to a client and the client is listening for it. Also, you cannot store data through the player. You need data stores for this. That is why data ready is deprecated. Also, if it was not deprecated, you got loading the values wrong. Edit: For an example of using remote events on OnClientEvent, we could make something that prints on the client, not the server, so the client can see it with F9.

--Server script:
game.Players.PlayerAdded:Connect(function() --cuz why not
	game.ReplicatedStorage.PrintToClient:FireAllClients('Hello, a server made this message to print to all the clients!')
end)

--LocalScript
game.RepllicatedStorage.PrintToClient.OnClientEvent:Connect(function(message)
	print('The server sent a message : '..message)
end)

This will print "The server sent a message : Hello, a server made this message to print to all the clients!"

2 comments

0

Thank's for the info, could you update the script showing an example of using an OnClinetEvent

0

Sure!

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