Problem with Remote Events

I am making a shop which is supposed to give an item. It doesn’t substract money so that doesn’t matter anymore, my issue is that the tools from replicatedstorage dont work when they are given. They only work in StarterPlayer. I already posted and I was told to make a RemoteEvent. I don’t have scripting knowledge so I tried doing this from a tutorial: Why does it not work? What do I have to change?

LocalScript on button:

-- Function to handle the purchase button click
local function PeriastronClicked()
	local player = game.Players.LocalPlayer

	-- Get the player's leaderstats
	local leaderstats = player:WaitForChild("leaderstats")

	-- Get the player's money value
	local moneyValue = leaderstats:WaitForChild("Money")

	-- Check if the player has enough money to make the purchase
	if moneyValue.Value >= 5000 then -- Adjust the price of the item here
		-- Subtract the item price from the player's money
		moneyValue.Value = moneyValue.Value - 10 -- Adjust the price of the item here

		-- Give the item to the player
		local item = game.ReplicatedStorage.Items.RainbowPeriastron:Clone()-- item
		item.Parent = player.Backpack
	else
		-- Player doesn't have enough money, display an error message or take appropriate action
		print("Not enough money to purchase the item!")
	end
end

-- button name
local Periastron = script.Parent -- Reference to the button itself

-- Connect the button click event to the purchaseButtonClicked function
Periastron.Activated:Connect(PeriastronClicked)

game.ReplicatedStorage.Items.RemoteEvent:FireServer(PeriastronClicked) 





--ServerScriptService:

game.ReplicatedStorage.RemoteEvent.OnserverEvent:Connect(function)

end)
1 Like

I dont think you understand how Parameters work, or how functions work.
When using functions, you need to have a Parameter in order to have an Argument, because here you arent giving a Parameter for the Argument to fill.

If you want to pass an argument, you give a function a parameter, which are these variables within the parenthesis:

function ex(parameter1, parameter2) -- Parameters in a function
    print(parameter1, parameter2) -- prints Parameter
end

local argument1 = 100 -- argument is the value that takes the place of the parameter
local argument2 = 200

ex(argument1, argument2) -- 100, 200
-- 100 takes the place of 'parameter1', Argument1
-- 200 takes the place of 'parameter2', Argument2

If you give them no value, they will return as nil, which means no value, if this ever happens you are able to automatically assign a value if that is what you want to happen with your code.

RemoteEvents However are slightly different, when used their first parameter is already filled with the Player as the Argument, so when fired, it would be ignored, and the first argument under :FireServer() would be given to the Second Argument, this is not the case with :FireClient() as it requires you to give the Player you want to fire the RemoteEvent to,so for the case of RemoteEvents firing to the Server, you can just say this:

RemoteEvent.OnServerEvent:Connect(function(player, arg1)
    print(player) -- the Player is already given when firing from Client to Server
    warn(arg1) -- will print the second argument, which in :FireServer() is the first
end)

RemoteEvent:FireServer(100) --> Player, 100

Your Method is also crazy unsafe, The Client cannot be trusted when handling these types of Data as it can be easily manipulated by exploiters, and completely useless to do according to the Server, another thing to mention you arent firing the function properly, Instead of telling the RemoteEvent to fire when the button is clicked, you are giving it the function to fire, which is not how that works, RemoteEvents when connected to OnServerEvent will fire to the Server, if you are handling this on the Client and not telling it to send the Data to the Server (dont Recommend unless you are handling the Data properly), It will only fire for the Client, where the change would only affect the Players Screen instead of everyone elses, if you have DataStores, they will not see this change and so they wont save the Data.
Basically do this:

Periastron.Activated:Connect(function()
    RemoteEvent:FireServer() -- fires directly to Server when button is Activated
end)

Please do your Research about RemoteEvents.

2 Likes

Thank you for your time, Im an extremely bad scripter and this information you gave me is worth gold for me.

2 Likes

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