[SOLVED] How to make different type of scripts communicate

Hello-

I am trying to make a system where if a player Activates a TextButton, it will depreciate their leaderstats value through DataStore2. Right now, I attempted to create an event that carries information over to a script which is where the cash depreciation will occur. However, I am having a hard time with the two different types of scripts communicating.

Local script - Handles the TextButton.Activated

local items = script.Parent:GetChildren()
local player = game.Players.LocalPlayer
local removeCashEvent = game.ReplicatedStorage:WaitForChild("CafeRemoteEvents").RemoveCash

for _, v in pairs(items) do
	if v:IsA("TextButton") then
		v.Activated:Connect(function() 
			if player.leaderstats.Cash.Value >= v.Price.Value then
				local cashAmount = v.Price.Value
				removeCashEvent:FireServer(cashAmount, player)
			else
				print("You do not have enough cash.")
			end
		end)
	end
end

Server script - Handles removing players’ cash

local removeCashEvent = game.ReplicatedStorage:WaitForChild("CafeRemoteEvents").RemoveCash

local DataStore2 = require(game.ServerScriptService:WaitForChild("Datastore").MainModule)
local defaultValue = 100
local dataStoreKey = "TestServerKey"

removeCashEvent.OnServerEvent:Connect(function(cashAmount, player)
	local cashDataStore = DataStore2(dataStoreKey,player) 
	cashDataStore:Increment(cashAmount,defaultValue)
end)

Can someone explain to me how to make this work? Thanks!

1 Like

You use Remote Events to do this. You call one in the cliant to the server by RemoteEvent:FireServer(--Variables in hear), RemoteEvent.OnServerEvent:Connect(function(player --only needed when passing variables, --variables here) and server to cliant RemoteEvent:FireCliant(player --always needed here, --variables here, RemoteEvent.OnCliantEvent:Connect(function(--variables here).

Please note that these are examples that are meant to teach you how to use them and not the exact code!

I’m aware - thanks for the information. I’m still not quite sure how to translate that into the code that I did but the information is nice to have.

Right now, your issue is the parameters. When using “FireServer”, the first argument by default is the player.

1 Like

I switched the parameter so it looks like this:

removeCashEvent:FireServer(player, cashAmount)

FireServer(player, cashAmount)

Still doesn’t seem to be working

You don’t need the player argument in your local script, it has the player argument by default.

So,

removeCashEvent:FireServer(cashAmount)

FireServer(player, cashAmount)

I did this and it works! Thanks so much for the patience with me I really appreciate it!

1 Like

Okay, here is a sample of code;
Local script

local RemoteEvent = game.ReplicatedStorage.RemoteEvent --You would add a remote event in replicated storage and replace this .RemoteEvent to ".YOURNAMEHERE"
local ToPrint = "Hello World!" --what will print in the server

RemoteEvent:FireServer(ToPrint) --Fires to server

Intercepting the event on the server;
Server script

local RemoteEvent = game.ReplicatedStorage.RemoteEvent -- again you can change this to the name of your event

RemoteEvent.OnServerEvent:Connect(function(player, ToPrint) --intercepts the event
    print(ToPrint) --prints "Hello World!" in the output menu!
end)
1 Like

I learned something today! Thanks this is great.

1 Like