After the purchase, the money is returning back

For example, my balance is 2000, I buy a map and I have 1000 coins left, then I get 5 coins and it becomes 2005

Here’s what’s happening to me:
https://cdn.discordapp.com/attachments/941709005695373323/954344165020430376/2022-03-18_14-39-18.mp4

this is cause you may be changing the code client-sided and not server-sided try using remote events to do so

1 Like

Can I have an example? I do not understand you

so lets say coins are 10 ok? if i change it to 20 using a localscript in startergui then it will show 20 for me but for the server it will remain 10 now if the server adds 1 to it it will become 11 instead of 21 but if u change it to 20 using a normal script it stays 20 for every single person in the server.

Hope this makes u understand

Show the code, we don’t know for sure if we don’t see it.

local RESET_SECONDS = 1
local isTouched = false
local Rep = game:GetService("ReplicatedStorage")
local Buy = Rep:WaitForChild("Buy")

local player = game:GetService("Players").LocalPlayer
function Hi()
	if script.Parent.Teleport0.Value == 1 then
		script.Parent.Visible = false
		wait(0.1)
		script.Parent.Parent.Lobby1.BackgroundTransparency = 0.5
		script.Parent.Parent.Lobby1.TextTransparency = 0
	elseif player.leaderstats.Coins.Value >= 100 then
		player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - 100
		script.Parent.Visible = false
		wait(0.1)
		script.Parent.Parent.Lobby1.BackgroundTransparency = 0.5
		script.Parent.Parent.Lobby1.TextTransparency = 0
		script.Parent.Teleport0.Value = 1
	else
		if not isTouched then
			isTouched = true
			script.Parent.Parent.Parent.Error.TextTransparency = 0
			wait(3)
			script.Parent.Parent.Parent.Error.TextTransparency += 0.1
			wait(0.1)
			script.Parent.Parent.Parent.Error.TextTransparency += 0.1
			wait(0.1)
			script.Parent.Parent.Parent.Error.TextTransparency += 0.1
			wait(0.1)
			script.Parent.Parent.Parent.Error.TextTransparency += 0.1
			wait(0.1)
			script.Parent.Parent.Parent.Error.TextTransparency += 0.1
			wait(0.1)
			script.Parent.Parent.Parent.Error.TextTransparency += 0.1
			wait(0.1)
			script.Parent.Parent.Parent.Error.TextTransparency += 0.1
			wait(0.1)
			script.Parent.Parent.Parent.Error.TextTransparency += 0.1
			wait(0.1)
			script.Parent.Parent.Parent.Error.TextTransparency += 0.1
			wait(0.1)
			script.Parent.Parent.Parent.Error.TextTransparency += 0.1
			wait(RESET_SECONDS)
			isTouched = false
		end
	end
end

script.Parent.MouseButton1Click:Connect(Hi)

Is there something or not? or a mistake

How can I do that? How can I do that? I’m sorry but I don’t know that well lua

use remote events :/

1 Like

I mean where and when to use it. Can I have an example?

local RESET_SECONDS = 1
local isTouched = false
local Rep = game:GetService("ReplicatedStorage")
local Buy = Rep:WaitForChild("Buy")

local player = game:GetService("Players").LocalPlayer
function BuyItem()
	if script.Parent.Teleport0.Value == 1 then
		script.Parent.Visible = false
		wait(0.1)
		script.Parent.Parent.Lobby1.BackgroundTransparency = 0.5
		script.Parent.Parent.Lobby1.TextTransparency = 0
	elseif player.leaderstats.Coins.Value >= 100 then
		player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - 100 -- here
		script.Parent.Visible = false
		wait(0.1)
		script.Parent.Parent.Lobby1.BackgroundTransparency = 0.5
		script.Parent.Parent.Lobby1.TextTransparency = 0
		script.Parent.Teleport0.Value = 1
	else
		if not isTouched then
			isTouched = true
			script.Parent.Parent.Parent.Error.TextTransparency = 0
			wait(2.9)
			for i=1,10 do
				wait(0.1)
				script.Parent.Parent.Parent.Error.TextTransparency += 0.1
			end
			wait(RESET_SECONDS)
			isTouched = false
		end
	end
end

script.Parent.MouseButton1Click:Connect(BuyItem)

i also fixed the code that you made

Players.CotRobloxGame.PlayerGui.Maps.Menu.Buy.Script:13: attempt to index nil with ‘leaderstats’

add a :WaitForChild around the leaderstats

What to use Local Script or Normal Script

Here is a new error: Players.CotRobloxGame.PlayerGui.Maps.Menu.Buy.Script:7: attempt to index nil with ‘WaitForChild’

give me the code that gives you the error

local RESET_SECONDS = 1
local isTouched = false
local Rep = game:GetService("ReplicatedStorage")
local Buy = Rep:WaitForChild("Buy")
local player = game:GetService("Players").LocalPlayer
local stats1 = player:WaitForChild("leaderstats")
function BuyItem()
	if script.Parent.Teleport0.Value == 1 then
		script.Parent.Visible = false
		wait(0.1)
		script.Parent.Parent.Lobby1.BackgroundTransparency = 0.5
		script.Parent.Parent.Lobby1.TextTransparency = 0
	elseif stats1.Coins.Value >= 100 then
		stats1.Coins.Value = stats1.Coins.Value - 100 -- here
		script.Parent.Visible = false
		wait(0.1)
		script.Parent.Parent.Lobby1.BackgroundTransparency = 0.5
		script.Parent.Parent.Lobby1.TextTransparency = 0
		script.Parent.Teleport0.Value = 1
	else
		if not isTouched then
			isTouched = true
			script.Parent.Parent.Parent.Error.TextTransparency = 0
			wait(2.9)
			for i=1,10 do
				wait(0.1)
				script.Parent.Parent.Parent.Error.TextTransparency += 0.1
			end
			wait(RESET_SECONDS)
			isTouched = false
		end
	end
end

script.Parent.MouseButton1Click:Connect(BuyItem)

is it a localscript or a normal script

This is a normal script. (This is a message)

you cant get the local player on a normal script. What you should do is to create a RemoteEvent on ReplicatedStorage and then create a local script on the button and add this

local RESET_SECONDS = 1
local isTouched = false
local Rep = game:GetService("ReplicatedStorage")
local Buy = Rep:WaitForChild("Buy")
local localPlayer = game:GetService("Players").LocalPlayer
local stats1 = localPlayer:WaitForChild("leaderstats")
local parent = script.Parent

script.Parent.MouseButton1Click:Connect(function(player)
	if script.Parent.Teleport0.Value == 1 then
		script.Parent.Visible = false
		wait(0.1)
		parent.Parent.Lobby1.BackgroundTransparency = 0.5
		parent.Lobby1.TextTransparency = 0
	elseif stats1.Coins.Value >= 100 then
        game.ReplicatedStorage:WaitForChild("remote event name here"):FireServer(player)
		script.Parent.Visible = false
		wait(0.1)
		parent.Parent.Lobby1.BackgroundTransparency = 0.5
		parent.Parent.Lobby1.TextTransparency = 0
		parent.Teleport0.Value = 1
	else
		if not isTouched then
			isTouched = true
			parent.Parent.Parent.Error.TextTransparency = 0
			wait(2.9)
			for i=1,10 do
				wait(0.1)
				parent.Parent.Parent.Error.TextTransparency += 0.1
			end
			wait(RESET_SECONDS)
			isTouched = false
		end
	end
end)

after that add a normal script on serverscriptservice and do something like this

local rp = game:GetService("ReplicatedStorage")
local event = rp:WaitForChild("Click") --remote event name here

event.OnServerEvent:Connect(function(player)
	player:FindFirstChild("leaderstats"):WaitForChild("Coins").Value -= 100
end)

please make it a solution if it works

1 Like