Remote event returning a button as nil from client to server

Client:

script.Parent.Purchase.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.GiveTrail:FireServer(script.Parent.Trail.Value, script.Parent.Purchase)
end)

Server:

game.ReplicatedStorage.GiveTrail.OnServerEvent:Connect(function(player, trail, button)
	warn(player,trail,button)
end)

Output:

MyName, Trail, nil

(script.Parent.Purchase is a text button and returns nil, however firing the remote event with something other than the text button gets returned properly)

(Firing the event with a child of the button also returns nil)

How do I fix this?

2 Likes

print scriptpurchase inside mousebutton1click

It prints the button’s name as it should

Try passing script.Parent.Purchase.Name instead of script.Parent.Purchase

That returns the name properly, however I want to make the text change to “Purchased” instead of “Purchase” and I would need the button to do that.

try to make, inside the OnServerEvent connection, a local variable like that:

local actual_button = script.Parent:FindFirstChild(button)

the script.Parent would be the parent of Purchase, so make sure script.Parent is the parent of Purchase element. I think you cannot pass the Purchase instance because RemoteEvents cannot pass arguments as instance, due to how serialization of data works

the button would be the name of the button

Use a remote function instead. Invoke the server with the trail you want to buy, handle the purchase on the server, then return whether or not the purchase failed back to the client, then change the button’s text on the client if the purchase succeeded.

This could work; thanks for the help!

1 Like

Using Remote function or Remote event wouldn’t change/fix the problem he is facing at the moment

Well the issue is occurring because the button doesn’t exist on the server. It’s being created on the client in some form. Anything created on the client won’t be replicated to the server.

cody (@7z99) has given a solution to the problem. Thanks for helping though!

1 Like

It would work because if the trail was purchased, then the remote function would return true. On the client, I could check if the remote function returns true in order to change the text.

1 Like

However what would change if it were a remote function instead of a remote event? The problem would still be there, as he would try to search for something inside the client

Yes, but you’re trying to change the text, right? Not checking if it changed or not

A remote function would allow you to wait for the server to process the purchase, then return a status (whether the purchase failed or not) back to the client so the client is then able to change the button’s text accordingly. You wouldn’t have to modify anything in the UI on the server (which is generally bad practice anyway)

1 Like

Yes, that’s right. But he is trying to change the text’s button, right? Then what would change with rather using remote function? Remote functions return a value to be sent to the other side, but he is not trying to do this, but changing the text. But, yea, that’d be a solution to the problem, though there are many other solutions to this, ig.

He could pass the player argument, and then find the PlayerGui. He could pass the button name, instead of the instance, and then use FindFirstChild to check if it is there. Then, he would change the text.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.