Any help with splitting this script into 2?

So I have this code that client sided, that gives you a money tool depending on the money you put it, but I need it give the money value to the money tool, server sided, but im confused on how i would split this up, any help? Here is the script.

local currency = "Cash"	-- Change this to the currency you want to use

local button = script.Parent
local setter = button.Parent.AmountBox
debounce = false

function chaching()
	button.Image = "http://www.roblox.com/asset/?id=96539788"
	wait(1)
	button.Image = "http://www.roblox.com/asset/?id=96539352"
end

function errormessage()
	setter.ErrorText1.Visible = true
	wait(3)
	setter.ErrorText1.Visible = false
end

function notenough()
	setter.ErrorText2.Visible = true
	wait(3)
	setter.ErrorText2.Visible = false
end

function onClick()
	if debounce == false then
		local player = button.Parent.Parent.Parent.Parent
		if tonumber(setter.Text) and tonumber(setter.Text) % 1 == 0 and tonumber(setter.Text) >= 1 then
			local amount = setter.Text
			local cash = player.leaderstats:FindFirstChild(currency)
			if tonumber(amount) <= cash.Value or tonumber(amount) == cash.Value then
				cash.Value = cash.Value - tonumber(amount)
				wait()
				player.leaderstats.Cash.Value = player.leaderstats.Cash.Value 
				local bag = game.Lighting.Money:clone()
				bag.Parent = player:FindFirstChild("Backpack")
				bag.MoneyValue.Value = amount
				debounce = true
				spawn(chaching)
				wait(5)
				debounce = false
			else
			notenough()
			end
		else
			errormessage()
		end
	end
end

button.MouseButton1Click:connect(onClick)

You would fire a RemoteEvent from the server sending the information on the MoneyValue and then you would do an OnClientEvent function and pass the money value as a parameter.

Here are some resources

- Hope this helped.

I know remote events, but im just not really with it ig, Im on board I just dont understand THIS.

Does it have to be on a local script? As I was scanning through, I didn’t see anything that requires that its a local script.

EDIT: I missed something, oops, nevermind.

It already is a local script, but I need the part where it gives the ammount in the box to the money tool, to be server sided.

So, this is in the PlayerGui, right?

Correct. CharacterLimit30TextThing

Alrighty. You should use a Remote Event, like @histo_rical said.
As well as this, you should not clone the tool on the client. As well, don’t use Lighting for your storage, use Replicated Storage.

When using remote events, always assume the data sent has been tampered with. Check if the player has enough cash on the server and the client. In the server, after the sanity checks, you can clone the Tool, but put a IntValue in it. You can change the IntValues Value to the cash that has been put in.

You can then view the IntValue in scripts in the tool.

Could you give me an example of what I should put into the server script?

Like how to use the remote event or based on your code?

Correct, I dont know what the code should be for the server.

--Client
local currency = "Cash"	-- Change this to the currency you want to use

local button = script.Parent
local setter = button.Parent.AmountBox
debounce = false

function chaching()
    button.Image =     "http://www.roblox.com/asset/?id=96539788"
    wait(1)
    button.Image =      "http://www.roblox.com/asset/?id=96539352"
end

function errormessage()
    setter.ErrorText1.Visible = true
    wait(3)
    setter.ErrorText1.Visible = false
end

function notenough()
    setter.ErrorText2.Visible = true
    wait(3)
    setter.ErrorText2.Visible = false
end

function onClick()
    if debounce == false then
    	local player =     button.Parent.Parent.Parent.Parent
	if tonumber(setter.Text) and    tonumber(setter.Text) <= player.leaderstats.Cash.Value then
		    local amount = setter.Text
		    local cash =   player.leaderstats:FindFirstChild(currency)
	    	if tonumber(amount) <=    cash.Value or tonumber(amount) == cash.Value then
			    game:GetService("ReplicatedStorage").Remo   te:FireServer(tonumber(amount))
	    	else
		notenough()
		end
	else
		errormessage()
	end
end

end

button.MouseButton1Click:connect(onClick)






--Server
  game:GetService("ReplicatedStorage").Remote. OnServerEvent:Connect(function(player,rcash)
if rcash > player.leaderstats.Cash.Value   then return end
     local tool =    game:GetService("ReplicatedStorage").Tool:Clone()
     tool.Parent = player.Backpack
     tool.CashValue.Value = rcash
end)

Btw, you don’t need to use game:GetService(). Its just a personal preference of mine when writing code.

1 Like

I will test this right now, I surely hope this works.

“Attempt to call a nil value” error. No line or script hint.

Did you make the remote event, name it Remote and parent it to replicated storage?

Yes. Characterlimit30textthing

Could I possibly teamcreate you or something like that?

Sorry, I can’t team create.

I’m literally 24 hours away from the computer I use roblox studio on.

What about the tool? Did you change the game:GetService(“ReplicatedStorage”).Tool to .Money?

Im so confused. Character30limit

Looking at @mistrustfully code quickly:

game:GetService(“ReplicatedStorage”).Remote. OnServerEvent:Connect(function(player,rcash

should be:

game:GetService(“ReplicatedStorage”).Remote.OnServerEvent:Connect(function(player,rcash)

Also,

game:GetService(“ReplicatedStorage”).Remo te:FireServer(tonumber(amount))

should be

game:GetService(“ReplicatedStorage”).Remote:FireServer(tonumber(amount))