When a player clicks a button cash must be lowered

Hello,

So i’m working with remote functions lately so no exploiter can give themself cash. But the serverscript needs to check if the value of the player is equal to 300 and then a model will get cloned. And if the player doesn’t have enough cash to buy it it will print something in the output.

The only thing is that it doesn’t works the model is getting cloned but it is getting cloned if the player doesnt has enough cash and if the player does have enough cash.

Can someone help me with this?

Here’s the script;

wait(5)
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local createPartRequest = Instance.new("RemoteFunction")
createPartRequest.Parent = ReplicatedStorage
createPartRequest.Name = "CreatePartRequest"



local function onCreatePartRequested(player)
	
	local Cash = player:WaitForChild("leaderstats"):WaitForChild("Cash")
	local leaderstats = player.leaderstats
	
	if Cash.Value >= 300 then
		
		local tvclone = game.ReplicatedStorage.Tvs.OldTv.PartsOfOldTv:Clone()
		tvclone.Parent = game.Workspace
		
	else
		
		print("You don't have enough cash!")
	
	end
end

createPartRequest.OnServerInvoke = onCreatePartRequested

Do you have any error on the output?

No, but this is the local script;

wait(5)

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local createPartRequest = ReplicatedStorage:WaitForChild("CreatePartRequest")

script.Parent.MouseButton1Click:Connect(function()
	local tvclone = createPartRequest:InvokeServer(game:GetService("Players").LocalPlayer)
end)
1 Like

Hmm sorry, i only know how work a remote event, not a remote function

Why are you referencing the LocalPlayer inside here? The Player is already passed when you connect/watch for the OnServerInvoke function

Also you’re not decreasing the Cash.Value after you check if the Player has more than 300

wait(5)
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local createPartRequest = Instance.new("RemoteFunction")
createPartRequest.Parent = ReplicatedStorage
createPartRequest.Name = "CreatePartRequest"



local function onCreatePartRequested(player)
	
	local Cash = player:WaitForChild("leaderstats"):WaitForChild("Cash")
	local leaderstats = player.leaderstats
	
	if Cash.Value >= 300 then

		Cash.Value -= 300 --Subtract 300 from the Player's Cash

		local tvclone = game.ReplicatedStorage.Tvs.OldTv.PartsOfOldTv:Clone()
		tvclone.Parent = game.Workspace
		
	else
		
		print("You don't have enough cash!")
	
	end
end

createPartRequest.OnServerInvoke = onCreatePartRequested
1 Like

Then what should there needs to be?(I didn’t made that line)

Nothing, you’re not passing anything onto the RemoteFunction so it’d only give you the Player parameter when you call OnServerInvoke

Like this?;

wait(5)

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local createPartRequest = ReplicatedStorage:WaitForChild("CreatePartRequest")

script.Parent.MouseButton1Click:Connect(function()
	local tvclone = createPartRequest:InvokeServer() -- forgot to put this here my fault.
end)

No, you still need to call its function to able for the Event to receive the invoked call

wait(5)

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local createPartRequest = ReplicatedStorage:WaitForChild("CreatePartRequest")

script.Parent.MouseButton1Click:Connect(function()
	local tvclone = createPartRequest:InvokeServer()
end)

The tv is not getting cloned, when I change my cash to 300 instead 1.1 million and then buy it the amount of cash I have is 300 right? Well when I click the purchase button the cash is getting changed to 1.1 mllion again. Any idea how this is possible? And why is the model not getting cloned?

You’re most likely changing the Cash.Value from the client side, you have to change it on the server side for it to count

Ah, well atleast I know that hackers can’t give themself cash now LOL. But how do I change it on the server side?

1 Like

See where this is?

Find that on the top of your Studio Tabs, and click it once, and it should change to the server-side

Is this a Gui button or a clickdetector?

This is a gui button. You can see that on the event MouseButton1Click I suppose?

Ok, but it still doesn’t clones the model.

Well, I’m not sure there’s a proper/better way but:
You’ll have to get the player using a server script in that button to change the leaderstats.
Let’s say the button is in a GUI, and the server script is in that gui, to get the player we would do:

local Player = script.Parent.Parent.Parent

The first parent is the GUI
The second parent is the player’s GUIs
The third one is the player, we can basically get everything we need now

I got a localscript in the purchase button and a server script in serverscriptservice.

A RemoteFunction when receiving its function on the server-side would return back the Player who fired it

@henrydanger5472 I’d recommend adding print statements to check what prints & what doesn’t

1 Like

I did that and it prints after the clone but i don’t see the tv and I don’t know why.