Hey people. Today I decided to make a discord code system that gives the user 500 cash once the code is redeemed. I knew I would need to use like a RemoteEvent or something...

So I wrote this code...

CLIENT SIDE:


frame = script.Parent.Parent.TwitterCodeFrame
button = script.Parent
closebutton = frame.ImageButton
submit = frame.Submit
box = frame.CodeBox
code = "ROBLOXObby"
repstor = game.ReplicatedStorage
player = game.Players.LocalPlayer
repeat wait(1) until player and player:IsDescendantOf(game.Players)
remote = game.ReplicatedStorage.Functions.RemoteEvent

button.MouseButton1Click:Connect(function()
	frame:TweenPosition(UDim2.new(0.205,0,0.12,0))
	button.Visible = false
end)

closebutton.MouseButton1Click:Connect(function()
	frame:TweenPosition(UDim2.new(1.303,0,0.12,0))
	button.Visible = true
end)

submit.MouseButton1Click:Connect(function()
	if box.Text == code then
		submit.Text = "Code redeemed!"
		remote:FireServer(500)
		wait(3)
		submit.Text = "Submit"
	else
		submit.Text = "Invalid code!"
		wait(3)
		submit.Text = "Submit"
	end
end)

So what that code does is when the correct code is used, it sends the amount of cash the user will receive to the server.

So, I wrote this to intercept the FireServer() function.


--local PointHandler = require(script.Parent.Parent.Functions.PointHandler)
--local addpoints = PointHandler.addpoints
--local delpoints = PointHandler.delpoints
local rep = game.ReplicatedStorage
local Settings = require(rep.Settings.ConfigScript)
local DataStore = game:GetService("DataStoreService"):GetDataStore("ROBLOX_Obby")
local SaveTime = tick()
local keyfab = "lb-%s"
local admintab = {"ImGeekGrrl", "FreakingHulk"}
local remote = game.ReplicatedStorage.Functions.RemoteEvent
remote.OnServerEvent:Connect(function(p, ...)
	local stats = p.leaderstats
	local cash = stats.Cash.Value
	cash = cash+...
	print("Added cash.")
end)
function checkadmin(p, admins)
	for _,v in next, admins do
		if p.Name == v then
			return true
		else
			return false
		end
	end
end

--\\ Auto Save
local function Save()
	for i,v in pairs(game.Players:GetChildren()) do
		spawn(function()
			local key=keyfab:format(v.UserId)
			DataStore:SetAsync(key, {v.leaderstats["Cash"].Value})
			print("Successfully saved "..v.leaderstats["Cash"].Value.." in "..v.Name.."'s Credit account!")
		end)
	end 
end

--\\ Load
game.Players.PlayerAdded:Connect(function(player)
		
    local leaderstats = Instance.new("Folder", player)
    leaderstats.Name = "leaderstats"

    local cash = Instance.new("NumberValue", leaderstats)
    cash.Name = "Cash"
    cash.Value = 0

	local key=keyfab:format(player.UserId)
	local saveValue = DataStore:GetAsync(key)
	if saveValue ~= nil then
		cash.Value = saveValue[1]
		print("Successfully loaded "..player.Name.."'s  account!")
	else
		cash.Value = 0
		DataStore:SetAsync(key, {cash.Value})
		print("Successfully made "..player.Name.."'s account!")
	end
	
player.Chatted:Connect(function(msg)
		if msg:lower() == "save" then
			if checkadmin(player, admintab) then
				Save()
				print("Saved everyone's stats.")
			end
		end	
	end)
end)

--\\ Save
game.Players.PlayerRemoving:Connect(Save)

--\\ Auto Save
while wait(tonumber(Settings.DataStore.Autosave)) do
	Save()
end

The GUI works okay when I join the game and enter the code. Except the code is supposed to give you 500 cash when redeemed. When I checked my Cash value, it was still at 10,000...

How would I fix this bug?

Thanks,

FreakingHulk14 (FHGDev)

6 comments

0

SetAsync overwrites current values, UpdateAsync might be a better option as it looks at old and new values

0

It has worked for as long as I've been on roblox, so I'm sure it's not an issue.

0

you need to do on line 15 stats.Cash.Value = cash since cash is merely what the Value was before   ...in other irrelevant news, I would definitely not use this approach, an exploiter could fire this remote event with any amount as an argument and have an infinite amount of money in your game. You should have the server check the input instead of the client & check if it happened previously

0

Which code block?

0

server script (2nd one)

0

On the localscript, use the WaitForChild method when instantiating the remote event variable. The localscript is trying to assign the remoteevent to a variable before the serverscript has created it yet.