How to change this Local Script to Script?

Hello, I’m creating exchange GUI, but i don’t know how to transfer Local Script to Script.

local plr = game.Players.LocalPlayer
local id = 23713633
local cash

-- Pet Multiplier
function getMultiplier(Player, Multiplier2)
	local Multi = 0
	for i,v in pairs(Player.Pets:GetChildren()) do
		if v.Equipped.Value == true then
			Multi = Multi + v[Multiplier2].Value
		end
	end
	return Multi
end

-- Exchange Gui
script.Parent.Cash.FocusLost:Connect(function(input)
	cash = tonumber(script.Parent.Cash.Text)
	if cash then
		if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(plr.UserId,id) then
			script.Parent.Gems.Text = cash / 50 * 2 * getMultiplier(plr, "Multiplier2")
		else
			script.Parent.Gems.Text = cash / 50 * 1 * getMultiplier(plr, "Multiplier2")
		end
	end
end)

script.Parent.Done.MouseButton1Click:Connect(function()
	if plr.leaderstats.Cash.Value >= cash then
		plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value - cash
		plr.leaderstats.Gems.Value = plr.leaderstats.Gems.Value + script.Parent.Gems.Text
	end
end)

When I’m clicking Done Button then I get Gems and I lost Cash but when I leave, Gems and Cash don’t saves beacuse this is Local Script and I don’t know how can I change this to Server Script

3 Likes

To achieve this you can make a “RemoteEvent” in ReplicatedStorage then call it from the local script
like this for example:

game.ReplicatedStorage.RemoteEvent:FireServer(cash, gems, etc)

Then you have to create a script on the server side to get that information like this

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr, cash, gems, etc)
    plr:FindFirstChild("leaderstats"):FindFirstChild("Cash").Value -= cash
    plr:FindFirstChild("leaderstats"):FindFirstChild("Gems").Value += gems
end)
2 Likes

You are confused. I think you want to save data after you leave, and the fact that you can’t set values like that on the client as the server and other players won’t see the changes. Local Script runs on the client for the client only and can communicate with the server only with remote events and remote functions.

Data stores are used for saving data even after a player has left and for across server data storage.

Remote Events can allow client to server and server to client communication.

Read this for help:

3 Likes

Thanks for help! Now it’s works! Tysm!

2 Likes

Awesome, glad to help in some way
:smiley: :+1:

2 Likes

Whose help did you need? Who did help you the most? Pick the one that helped the most and press the solution box so others see this post has been solved. Also because we out there for those juicy solutions!

3 Likes

Keep in mind having a remote that adds money is a wide-open door for exploiters, since they can just send a fake remote. Maybe move this to the server, or, if you can’t, do checks on the server.

1 Like

Yeah, i agree, using remotes is pretty risky for your game.

1 Like

Only insecure remotes are really that dangerous, as long as they’re secured properly and don’t carry information the server should handle in the first place, they should be fine.

1 Like