ReplicatedStorage and OnClientEvent

image

game.ReplicatedStorage.NeedMoney.OnClientEvent:Connect(function()

script.Parent.Visible = true

end)

image

script.Parent.Frame.Yes.MouseButton1Click:Connect(function()
		script.Parent:TweenPosition(UDim2.new(0.336,0,-1,0), "Out", "Quad", 1)
		local plr = game.Players.LocalPlayer
		if car == "Car1" then
			if plr.leaderstats.Money.Value < 5000 then
				game.ReplicatedStorage.NeedMoney:FireServer(plr)
			else
				game.ReplicatedStorage.Carhandler:FireServer(car)
			end
		end
		if car == "Car2" then

			if plr.leaderstats.Money.Value < 10000 then
				game.ReplicatedStorage.NeedMoney:FireServer(plr)
			else
				game.ReplicatedStorage.Carhandler:FireServer(car)

			end
		end
		if car == "Car3" then
			if plr.leaderstats.Money.Value < 20000 then
				game.ReplicatedStorage.NeedMoney:FireServer(plr)
			else
				game.ReplicatedStorage.Carhandler:FireServer(car)

			end
		end
		if car == "Car4" then
			if plr.leaderstats.Money.Value < 20000 then
				game.ReplicatedStorage.NeedMoney:FireServer(plr)
			else
				game.ReplicatedStorage.Carhandler:FireServer(car)
			end
		end
	end)
end)

This is not full script


Why it isn’t works?

Try using :GetService() instead.

local repl = game:GetService("ReplicatedStorage")
local rem1 = repl.rem1
local rem2 = repl.rem2 --and so on

How and in which of scripts? I am bad in using it

replace all your game.ReplicatedStorage to game:GetService(“ReplicatedStorage”) thats what he meant

On the top of all scripts that reference something in ReplicatedStorage, you would include local repl = game:GetService("ReplicatedStorage"), then anything inside ReplicatedStorage can be referenced with repl.something, replacing something with whatever you intend to reference of course.

maybe try this?

game.ReplicatedStorage.NeedMoney.OnSeeverEvent:Connect(function()

script.Parent.Visible = true

end)

also its reccomended to show full script

Yes, except using :GetService() every single time when it’s needed is inefficient. It’s better to call it once and assign it to a variable, like I did.

Can you show this with my script example?

im surprised that he used FireServer on the first script and he OnClientEvent on the second i just dont understand what he wants until he shows the server script

I just noticed both of them are LocalScripts, which means they can’t communicate directly without the server’s help. Maybe try reading this for help?

In the 2nd script you’re firing everything on the server, you need to receive the command by using

game.ReplicatedStorage.YourRemote.OnServerEvent

and not

game.ReplicatedStorage.YourRemote.OnClientEvent

There’s a few things wrong with this. Firstly, you’re sending a :FireServer() with the Player as a parameter. A RemoteEvent will always fire to server with player as the first argument, without you having to send it yourself.

RemoteEvent:FireServer()

-- what the server receives
RemoteEvent.OnServerEvent:Connect(function(player)
    ...
end)

Also, I see you are firing an event to the server with the remote, then I’m confused on are you trying to read that fire from a LocalScript? Or is that feedback? If it’s feedback, use a RemoteFunction instead. Otherwise, the way remotes work is that you send a :FireServer() command to the server, and in a regular Script you’ll have:

RemoteEvent.OnServerEvent:Connect(function (player, ...)
     ...
end)

All in all, if you’re going to be using RemoteEvents at all, I suggest handling this all in the server to begin with. Don’t calculate in the client and instead Fire what you want to buy, and let the server calculate if that’s possible:

-- client
RemoteEvent:FireServer("Car1") -- request to buy Car 1 to server...

-- server
RemoteEvent.OnServerEvent:Connect(function (player, request)
    if request == "Car1" then
        if enoughMoney(player) then -- where enoughMoney is the way to determine if there is enough money in the player
            giveCarToPlayer(player) -- where you handle giving the purchase
        end
    end
end)

This is more of a template-style pseudocode, but it shows how you’d manage server-sided calculations to ensure that the client isn’t lying about how much money they have.

If you’re not sure how client-server interactions work read article about client/server interactions.