Better way to send Money

Hello! I have a event where it sends money, but I know there’s a better way to do it, I don’t want to manually write it.

Local Script

local rs = game:GetService("ReplicatedStorage")
local Shapes = rs:WaitForChild("Shapes", 60):GetDescendants()

local Spawner = workspace:WaitForChild("Spawner")
local MoneyEvent = rs.MoneyEvent
local Chance = require(rs:WaitForChild("Chance"))

local gui = script.Parent
local Button = gui.ShapeButton

local HasShape = false
local OldShape = nil

local MoneyPoints = 0

print("Everything loaded.")

Button.MouseButton1Down:Connect(function()
	local Rarity = Chance.PickRarity("Rarities")
	print(Rarity)
	for i, v in pairs(Shapes) do
		if v:IsA("BasePart") or v:IsA("MeshPart") or v:IsA("UnionOperation") then
			local Folder_Shapes = v.Parent

			if Rarity == Folder_Shapes.Name then

				local RandomShape = Folder_Shapes:GetChildren()[math.random(1, #Folder_Shapes:GetChildren())]:Clone()

				if HasShape ~= true then
					HasShape = true
					
					RandomShape.Parent = Spawner
					RandomShape.CFrame = CFrame.new(Spawner.CFrame.X, Spawner.CFrame.Y, Spawner.CFrame.Z)
					
					print(RandomShape)
					OldShape = RandomShape
					
					if Folder_Shapes.Name == "Common" then
						MoneyPoints += 10
						MoneyEvent:FireServer(MoneyPoints)
					end
					
					if Folder_Shapes.Name == "Rare" then
						MoneyPoints += 30
						MoneyEvent:FireServer(MoneyPoints)
					end
					
					break

				elseif HasShape == true then
					
					OldShape:Destroy()
					HasShape = false
				end
			end
		end

	end

end)

Module Script

local Chance = {}

local Rarities = {
	Common = 0, -- 60% chance
	Rare = 0.6, -- 40% chance
	Epic = 0.8, -- 20% chance
	Legendary = .9, -- 10% chance
	-- Mythic = 0.99, -- 1% chance
	-- Unique = 0.991, -- 0.09% chance
	-- Ultra = 0.995, -- 0.05% chance
	-- Insane = 0.999 -- 0.01% chance
}



function Chance.PickRarity()
	local Index = math.random()
	local HighestRarity = "Common"

	for RarityName, Value in pairs(Rarities) do
		if Index >= Value and Value >= Rarities[HighestRarity] then
			HighestRarity = RarityName
		end
	end

	return HighestRarity
end

return Chance

I still can’t think of a way to improve this system

It seems like the best way to do things, although when defining rarities, it is best to avoid longer decimals due to float point inaccuracies that could cause problems in future code.

1 Like

The client shouldn’t have the authority to award cash as it can be easily exploited.

One approach is to link a remote function to a button click event. When the button is clicked, the server handles the reward calculation and adjusts the player’s cash accordingly.

It’s advisable to utilize a service like Knit for smoother communication between the client and server, ensuring that the player’s money on the client side matches that on the server side.

Alternatively, another option is to set up a system where the server updates information that the client can access. For instance, create a folder containing a list of users, with each user having an IntegerValue connected named “Money” reflecting their cash amount. The server updates the player’s cash in their datastore and updates this IntegerValue. Then, the client can monitor changes (using :Changed method) to this IntegerValue to update any UI elements or cash displays accordingly.

2 Likes

Great suggestion! I’ll most likely rewrite the whole datastore code, and other stuff.

1 Like

Would recommend profileservice - Great resource that will massively simplify creating a secure datastore

Here’s the devforum post which contains ProfileService and information on how it works:

This video should be a massive help as well:

1 Like

Funny thing! I rewrote the whole thing in profile service; although took 4 hours to change everything. Thanks for the vid recommendation!

1 Like

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