Sending data from local script to server script

Greetings,

I am not sure why data is not sent through my local script to the server script. I am talking about player, amount and receiver as said on re:FireServer(player, amount, receiver) . My code is down below:

script.Parent.MouseButton1Click:Connect(function(player)
	local player = script.Parent.Parent.Parent.Parent.Parent
	print(player.Name.. " is the sender")
	local cash = player:WaitForChild("leaderstats"):WaitForChild("Cash")
	local receiver = script.Parent.Parent.Player.Text
	local amount = script.Parent.Parent.Amount.Text
	local rs = game:GetService("ReplicatedStorage")
	local re = rs:WaitForChild("Sending"):WaitForChild("Send")

	if game.Players:FindFirstChild(receiver) then
		print("Receiver is: " ..receiver)
		re:FireServer(player, amount, receiver) 
		print("Remote event for sending has been triggered.")
	elseif not game.Players:FindFirstChild(receiver) then
		print("Receiver " ..receiver.. " not found.")
	end
end)

I am a beginner with remote events and I don’t quite get them. Please help, thanks.

2 Likes

May I see your server script?
So i can check how are you getting event connection.

2 Likes

The event is working just fine, but here it is:

local rs = game:GetService("ReplicatedStorage")
local re = rs:WaitForChild("Sending"):WaitForChild("Send")

re.OnServerEvent:Connect(function(player)
	print("Remote event for sending has been received.")
end)

You are adding an extra parameter right here, the server script automatically receives the player that fired the event.

And, since this is a local script, just use game.Players.LocalPlayer

1 Like

Oh ok I’ll remove it. character limit

local rs = game:GetService("ReplicatedStorage")
local re = rs:WaitForChild("Sending"):WaitForChild("Send")

re.OnServerEvent:Connect(function(localPlayer, sender, amount, receiver)
	print("Remote event for sending has been received.")
    print(sender, amount, receiver)
end)
Fixed
LocalScript
script.Parent.MouseButton1Click:Connect(function(player)
	local player = game.Players.LocalPlayer
	print(player.Name.. " is the sender")
	local cash = player:WaitForChild("leaderstats"):WaitForChild("Cash")
	local receiver = script.Parent.Parent.Player.Text
	local amount = script.Parent.Parent.Amount.Text
	local rs = game:GetService("ReplicatedStorage")
	local re = rs:WaitForChild("Sending"):WaitForChild("Send")

	if game.Players:FindFirstChild(receiver) then
		print("Receiver is: " ..receiver)
		re:FireServer(amount, receiver) 
		print("Remote event for sending has been triggered.")
	elseif not game.Players:FindFirstChild(receiver) then
		print("Receiver " ..receiver.. " not found.")
	end
end)
Server Script
local rs = game:GetService("ReplicatedStorage")
local re = rs:WaitForChild("Sending"):WaitForChild("Send")

re.OnServerEvent:Connect(function(localPlayer, amount, receiver)
	print("Remote event for sending has been received.")
    print(localPlayer.Name.." is the sender, "..receiver.." is the receiver with the amount "..amount)
end)
5 Likes

Everything works, thanks a lot! But how do I make it so that only the receiver and the sender can run the script?

Obviously, after the event has been received, to make a if-then statement and if the player name = to the receiver then the script continues.

Remote events already have a in-built feature where you don’t have to worry about who used to remote event.

When a remote event is fired, the callback gets LocalPlayer variable (the player who triggered the remote event.)

Example

-- Lets imagine this is me clicking a button.
button.MouseButton1Click:Connect(function()
    event:FireServer() -- fires the event to the server, event sends us to the server by default, without mentioning ourself.
end)

event.onServerEvent:Connect(function(player)
   player.leaderstats.Cash.Value += 100 -- Adds cash value to us.
end)

meanwhile me:
Synitx: 100 Cash
while my friend who havent clicked the button.
Friend: 0 Cash

So you don’t have to check for receiver, because the receiver is already being passed by the remote event that is our player (You)

Well i am not great at teaching but here look at these few vids and articles.

https://developer.roblox.com/en-us/api-reference/class/RemoteEvent
Video by @Alvin_Blox

1 Like

Thank you a lot once again, I will look into it!

1 Like