Problem with replication of cash when a button is pressed

Hello programmers or coders? Anyways I have stumbled upon a problem! so let me tell you the main stuff!

there are 2 main buttons for this incident:

  1. button-1
    generates 1$ every touch
  2. button-2
    generates 10$ every touch

so, the problem starts here so first for all if you need to purchase button-2 you need to spend 1000$ to get button2 now here the real problem starts so if you spend 1000$ and press on it. rather than getting 10$ you would GET 1000$, LIKE FROM WHERE? yeah so I really don’t understand why it is getting replicated? rather than getting 10$ player is getting 1000$ because they spend that much amount! So, can you resolve this issue? i have tried many ways but still it did not work a little too. :frowning:

SO here is the button code!:

local playerService = game:GetService("Players")
local button = script.Parent.redbutton

local bounce = false
local function onTouched(hit)
	local character = hit.Parent
	local player = playerService:GetPlayerFromCharacter(character)

	if player then
		bounce = true
		local leaderstats = player:FindFirstChild("leaderstats")
		if leaderstats then
			local cash = leaderstats:FindFirstChild("Cash")
			if cash and not bounce then
				bounce = true
				local pass2x = player.ButtonsFolder.Cash2x.Value
				local pass3x = player.ButtonsFolder.Cash3x.Value
				local pass5x = player.ButtonsFolder.Cash5x.Value

				cash.Value = cash.Value + 10 
				print(cash.Value)

				task.wait(0.1)
				bounce = false
			end
		end
	end
end

while true do
	for _, part in ipairs(button:GetTouchingParts()) do
		task.spawn(onTouched, part)
	end

	task.wait()
end

Any idea how to fix it?

Is there any other scripts? Like where you make the script for buying the button? There’s nothing to do with this script that would cause it to give 1000

yeah it is there it is in local script:
Please check it out and let me know if there are any errors!

local remoteevent = game.ReplicatedStorage:WaitForChild("magnitudebt")
local verfi = game.ReplicatedStorage:WaitForChild("VerificationEvent")
local buttongui = script.Parent
local button2 = game.Workspace.Buttonsim.Buttonsfolder.Buttons2:WaitForChild("Barrierb3")
local player = game.Players.LocalPlayer
local click = false

buttongui.Yes.MouseButton1Up:Connect(function()
	click = true
	
	if player then
		local leaderstats = player:FindFirstChild("leaderstats")
		if leaderstats then
			local cash = leaderstats:FindFirstChild("Cash")
			if click == true and cash.Value >= 1000 then
				cash.Value = cash.Value - 1000
				button2.Transparency = 1
				button2.CanCollide = false
				click = false
				if click == not true then
					-- sends the remote event to magnitude script to stop the script
					remoteevent.magnittruebt2:FireServer(true)
					buttongui.Visible = false
				end
				if button2.Transparency == 1 and button2.CanCollide == false then
					buttongui.Visible = false
				else 
					buttongui.Visible = true
				end
			end
		end
	end
end)

buttongui.No.MouseButton1Up:Connect(function()
	buttongui.Visible = false
end)

Try firing a remote event to the server to handle the purchase there, it won’t replicate if it’s in client

buttongui.Yes.MouseButton1Up:Connect(function()
	click = true
	
	if player then
		local leaderstats = player:FindFirstChild("leaderstats")
		if leaderstats then
			local cash = leaderstats:FindFirstChild("Cash")
			if click == true then
				purchaseEvent:FireServer(button2) -- purchaseEvent is a new remote event
click = false
				if click == not true then
					-- sends the remote event to magnitude script to stop the script
					remoteevent.magnittruebt2:FireServer(true)
					buttongui.Visible = false
				end
				if button2.Transparency == 1 and button2.CanCollide == false then
					buttongui.Visible = false
				else 
					buttongui.Visible = true
				end
			end
		end
	end
end)

Server (script in ServerScriptService):

purchaseEvent.OnServerEvent:Connect(function(player, button2) -- again purchaseEvent is that new remote event
local leaderstats = player:FindFirstChild("leaderstats")
		if leaderstats then
			local cash = leaderstats:FindFirstChild("Cash")
			if cash.Value >= 1000 then
				cash.Value = cash.Value - 1000
				button2.Transparency = 1
				button2.CanCollide = false
				click = false
end
end
end)

Yup I will surely check it out!

@neweve2323
hey i want only the client button2 gets removed rather than whole server! and it does remove but it will remove for everyone!

Sorry for that, I changed the script again to only make the button invisible for the client:

buttongui.Yes.MouseButton1Up:Connect(function()
	click = true

	if player then
		local leaderstats = player:FindFirstChild("leaderstats")
		if leaderstats then
			local cash = leaderstats:FindFirstChild("Cash")
			if click == true then
				purchaseEvent:FireServer() -- purchaseEvent is a new remote event
				if cash.Value >= 1000 then
					button2.Transparency = 1
					button2.CanCollide = false
					click = false
				end
				if click == not true then
					-- sends the remote event to magnitude script to stop the script
					remoteevent.magnittruebt2:FireServer(true)
					buttongui.Visible = false
				end
				if button2.Transparency == 1 and button2.CanCollide == false then
					buttongui.Visible = false
				else 
					buttongui.Visible = true
				end
			end
		end
	end
end)

Server:

purchaseEvent.OnServerEvent:Connect(function(player) -- again purchaseEvent is that new remote event
local leaderstats = player:FindFirstChild("leaderstats")
		if leaderstats then
			local cash = leaderstats:FindFirstChild("Cash")
			if cash.Value >= 1000 then
				cash.Value = cash.Value - 1000
				end
end
end)

will it only deduct from player cash? then it is ok

@neweve2323
hey man thanks for the help i was doing it for whole day!!!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.