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.
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)
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)
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.