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

Player from server to client [REMOTE EVENT]?

So basically, I use an playerAdded event, to check when someone joins. When they join the should get a GUI, but it seems like it doesnt work to get the player from the even to see the GUI.

Server script:

game.Players.PlayerAdded:Connect(function(player)
	local key = tostring(player.UserId)
	local LastLogin = DataStore:GetAsync(key)
	local seconds = os.time()
	local minutes = seconds/60
	local hours = minutes/60
	
	if DataStore then
	if seconds >= 24 then
		print(player.leaderstats.Points.Value)
		local amount = math.random(85,100)
		DataStore:SetAsync(key, os.time())
		player.leaderstats.Points.Value = player.leaderstats.Points.Value + amount
		game.ReplicatedStorage.DailyRewardSystem:FireClient(player, amount)
				print(player.leaderstats.Points.Value .. " old")
		print(amount)
		
	end
	else
		DataStore:SetAsync(key, os.time())
	end
end)

--Local script:

game.ReplicatedStorage.DailyRewardSystem.OnClientEvent:Connect(function(player, amount)
	
		print(amount)
		print(player)
		player.PlayerGui.ScreenGui.PointsAwarded.Amount.Text = amount
		player.PlayerGui.ScreenGui.PointsAwarded.Name.Text = ("Hey " .. player.Name .. "!")
		player.PlayerGui.ScreenGui.PointsAwarded.Visible = true
		wait(3)
		player.PlayerGui.ScreenGui.PointsAwarded.Visible = false
	
	
	
end)

The players prints the amount and the amount is a nil value. How can I get the specific player that joined from the server to the client? Thanks.

1 answer

#OnClientEvent; No Player OnClientEvent doesn't pass the player. The server fires OnClientEvent, and the server isn't a player.

Removing the player argument should make this work. Use LocalPlayer from a local script if you need the player.

local player = game:GetService("Players").LocalPlayer
local pointsAwarded = player.PlayerGui.ScreenGui.PointsAwarded

game.ReplicatedStorage.DailyRewardSystem.OnClientEvent:Connect(function(amount)
    print(amount, player)
    pointsAwarded.Amount.Text = amount
    pointsAwarded:FindFirstChild("Name").Text = "Hey " .. player.Name .. "!"
    -- Properties have precedence over children, so Name will be the property, not the text label
    pointsAwarded.Visible = true
    wait(3)
    pointsAwarded.Visible = false
end)

And to fire to the player, call RemoteEvent:FireClient(player, ...).


######Hopefully this answered your question, and if it did, then don't forget to hit that accept button. If you have any more questions then feel free to leave them down in the comments.

##Edit:

OnClientEvent = ... FireClient = player, ...

5 comments

0

Do you think this will work? SInce I want the specific player that joined...

0

And when I remove the player argument from the :FireClient() it outputs; Unable to cast value to object...

0

You need to pass the player as first argument to FireClient

0

So i only need to pass the argument from the server, and then on client; get the local player?

0

yeah

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