Is my script for dice good?

Server script:

local DiceEvent = game:GetService("ReplicatedStorage").Events.AuraAndDice.Dice
local EventGive = game:GetService("ReplicatedStorage").Events.AuraAndDice.EventGive
local HugeAura = game:GetService("ReplicatedStorage").Events.AuraAndDice.HugeAura
local MusicoEvent = game:GetService("ReplicatedStorage").Events.AuraAndDice.Musico
local giveaura = require(game.ServerScriptService.Module.GiveAura)

local function Rolls(auramsg, character)
	local auras = require(game:GetService("ServerScriptService").Module.AurasList)
	local randomnumber = math.random()
	local luckmodifier = character.Luck.Value
	local selectedAura
	for i, aura in pairs(auras) do
		local auraProbability = 1 / aura.OneIn
		if randomnumber * (1 + luckmodifier) <= auraProbability then
			auramsg.Text = i
			auramsg.TextColor3 = aura.TextColor or Color3.new(255,255,255)
			auramsg.UIGradient.Color = aura.Gradient or ColorSequence.new(Color3.new(255,255,255), Color3.new(255,255,255))
			auramsg.ChanceForAura.Text = "1 in " .. aura.OneIn
			selectedAura = selectedAura or i
			character:SetAttribute("OneInAura", aura.OneIn) 
			break
		else
			randomnumber = randomnumber - auraProbability
		end
	end
	wait(0.01)
end

local function SpinAndRoll(spinmsg, player, RepeatTime)
	local SpinTween = TweenService:Create(spinmsg, TweenInfo.new(RepeatTime, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {Position = UDim2.new(0.227, 0, 4.688, 0)})
	SpinTween:Play()
	Rolls(spinmsg, player.Character)
	MusicoEvent:FireClient(player)
	SpinTween.Completed:Wait()
	spinmsg.Position = UDim2.new(0.227, 0, 4.288, 0)
end

DiceEvent.OnServerEvent:Connect(function(player, QK)
	local character = player.Character
	if character:GetAttribute("isdice") == true then return end
	character:SetAttribute("isdice", true)
	local spinmsg = player.PlayerGui.Main.DiceScreen.SpinMSG.RolledAura
	local counter = 0
	local RepeatTime = 0.2

	if QK == true then
		SpinAndRoll(spinmsg, player, RepeatTime)
		counter = 9
	else
		repeat
			SpinAndRoll(spinmsg, player, RepeatTime)
			counter += 1
		until counter >= 9
	end

	if counter == 9 then
		spinmsg.Position = UDim2.new(0.227, 0, 4.688, 0)
		DiceEvent:FireClient(player)
		local aura = character:GetAttribute("OneInAura")
		if tonumber(aura) > 1000 then
			HugeAura:FireAllClients(player, aura, spinmsg)
		end
		character:SetAttribute("isdice", false)
		EventGive.OnServerEvent:Connect(function(player)
			local guiplayer = character.Head:WaitForChild("GUIPlayers")
			guiplayer.Aura.Text = spinmsg.Text
			giveaura:GiveAura(character, "Quarted")
		end)
	end
end)

Local Script:

--Services = {
local Player = game:GetService("Players").LocalPlayer
local DiceEvent = game:GetService("ReplicatedStorage").Events.AuraAndDice.Dice
local GiveEvent = game:GetService("ReplicatedStorage").Events.AuraAndDice.EventGive
local TweenService = game:GetService("TweenService")
--}
-- Variables = {
local antidebounce = false
local QuickDice = false
local AutoDice = false
local character = Player.Character or Player.CharacterAdded:Wait()
local dicegui = Player.PlayerGui.Main.DiceScreen
local localSettings = Player.PlayerGui.Main.Settings
--}
local TweenList = {
	["DiceTween"] = TweenService:Create(dicegui, TweenInfo.new(0.45), {Transparency = 0.7}),
	["DiceReturnTween"] = TweenService:Create(dicegui, TweenInfo.new(0.45), {Transparency = 1}),
	["SkipTween"] = TweenService:Create(dicegui.Skip, TweenInfo.new(0.5), {Position = UDim2.new(0.346, 0, 0.8, 0)}),
	["EquipTween"] = TweenService:Create(dicegui.Equip, TweenInfo.new(0.5), {Position = UDim2.new(0.546, 0, 0.8, 0)}),
	["SkipReturnTween"] = TweenService:Create(dicegui.Skip, TweenInfo.new(0.5), {Position = UDim2.new(0.346, 0, 1.5, 0)}),
	["EquipReturnTween"] = TweenService:Create(dicegui.Equip, TweenInfo.new(0.5), {Position = UDim2.new(0.546, 0, 1.5, 0)})
}

local function evade()
	TweenList["DiceReturnTween"]:Play()
	dicegui.Dice.Visible = true
	dicegui.Invertory.Visible = true
	dicegui.Settings.Visible = true
	dicegui.SpinMSG.Visible = false
	dicegui.SpinMSG.RolledAura.Position = UDim2.new(0.227, 0, 4.288, 0)
end

DiceEvent.OnClientEvent:Connect(function()
	local needed = true
	local OneIn = tonumber(character:GetAttribute("OneInAura"))
	local autoskip = tonumber(localSettings.AutoSkip.SkipBox.Text)
	local autoequip = tonumber(localSettings.AutoEquip.EquipBox.Text)
	if autoskip ~= 0 or nil then
		if autoskip > OneIn then
			evade()
			needed = false
			wait(0.5)
			antidebounce = false
		end
	end

	if autoequip ~= 0 or nil then
		if autoequip < OneIn then
			evade()
			needed = true
			GiveEvent:FireServer(Player)
			wait(0.5)
			antidebounce = false
		end
	end

	task.wait(0.15)
	if needed then
		TweenList["SkipTween"]:Play()
		TweenList["EquipTween"]:Play()
	end
end)

local function dice()
	if antidebounce then return end
	antidebounce = true
	TweenList["DiceTween"]:Play()
	dicegui.SpinMSG.RolledAura.Text = " "
	dicegui.SpinMSG.RolledAura.ChanceForAura.Text = " "
	dicegui.Dice.Visible = false
	dicegui.Invertory.Visible = false
	dicegui.Settings.Visible = false
	dicegui.SpinMSG.Visible = true
	DiceEvent:FireServer(QuickDice)
end

local function realdice()
	if AutoDice then
		while AutoDice do
			dice()
			wait(1)
		end
	else
		dice()
	end
end

dicegui.Dice.QuickDice.MouseButton1Up:Connect(function()
	QuickDice = not QuickDice
	if QuickDice then
		dicegui.Dice.QuickDice.ImageColor3 = Color3.new(0.14902, 1, 0)
	else
		dicegui.Dice.QuickDice.ImageColor3 = Color3.new(1, 0, 0.0156863)
	end
end)

dicegui.Dice.AutoDice.MouseButton1Up:Connect(function()
	AutoDice = not AutoDice
	if AutoDice then
		dicegui.Dice.AutoDice.ImageColor3 = Color3.new(0.14902, 1, 0)
		realdice()
	else
		dicegui.Dice.AutoDice.ImageColor3 = Color3.new(1, 0, 0.0156863)
	end
end)

dicegui.Skip.MouseButton1Up:Connect(function()
	evade()
	TweenList["SkipReturnTween"]:Play()
	TweenList["EquipReturnTween"]:Play()
	wait(1)
	antidebounce = false
	if AutoDice then realdice() end
end)

dicegui.Equip.MouseButton1Up:Connect(function()
	evade()
	TweenList["SkipReturnTween"]:Play()
	TweenList["EquipReturnTween"]:Play()
	GiveEvent:FireServer(Player)
	wait(1)
	antidebounce = false
	if AutoDice then realdice() end
end)

dicegui.Dice.MouseButton1Up:Connect(realdice)

I got huge disappointed, and i can’t get, is my script is good or something is wrong. It’s seeming ok but i still can’t understand good it or not

Your script for handling dice events seems quite functional! However, there are always areas where you can improve and optimize code.

Some suggestions for improving your scripts:

Server Script:

  1. Optimize Imports: Instead of importing each event separately, you can import the entire AuraAndDice namespace once.
  2. Avoid Redundant Require Calls: You can require the AurasList module outside the Rolls function to avoid redundant require calls.
  3. Use elseif: Instead of else followed by another if, use elseif.
  4. Instead wait() use task.wait(). It is more stable and optimized,

Local Script:

  1. Consistent Tween Naming: Ensure consistent naming for tween variables.
  2. Avoid Magic Numbers: Replace magic numbers with constants or variables for better readability and maintainability.
  3. Use not Directly: Instead of ~= false, use not.
  4. Consistent Naming: Ensure consistent naming conventions throughout the script.
  5. Use elseif: Instead of nested if statements, use elseif.
  6. Avoid Redundant Conditions: Conditions like or nil are redundant. nil is always false in Lua.
  7. Avoid Hardcoded Values: Use variables/constants for values that may change.
  8. Reuse Tween Creation: You can reuse the Tween creation code to avoid redundancy.
  9. Separate Concerns: Separate concerns by splitting functionalities into functions for better readability and maintainability.

General:

  1. Error Handling: Implement error handling where necessary, especially for critical operations.
  2. Comments: Add comments to explain complex logic or functionalities.
  3. Code Structure: Ensure consistent code structure and formatting for readability.
  4. Code Refactoring: Refactor repetitive code into functions to improve code reuse and maintainability.
1 Like

did you ai generate this, genuine question

by the way if we are talking about AI…
on server, function Rolls generated by AI, so pay attention to it.

AI gave the idea and I explained it

i did not understand a word you said :rofl:

you know what’s the strange part? you explaining to use not instead of ~= false when there wasn’t any ~= false used in his code or i’m just blind

local function Rolls(auramsg, character)
	local auras = require(game:GetService("ServerScriptService").Module.AurasList)
	local randomnumber = math.random()
	local luckmodifier = character.Luck.Value
	local selectedAura
	for i, aura in pairs(auras) do
		local auraProbability = 1 / aura.OneIn
		if randomnumber * (1 + luckmodifier) <= auraProbability then
			auramsg.Text = i
			auramsg.TextColor3 = aura.TextColor or Color3.new(255,255,255)
			auramsg.UIGradient.Color = aura.Gradient or ColorSequence.new(Color3.new(255,255,255), Color3.new(255,255,255))
			auramsg.ChanceForAura.Text = "1 in " .. aura.OneIn
			selectedAura = selectedAura or i
			character:SetAttribute("OneInAura", aura.OneIn) 
			break
		else
			randomnumber = randomnumber - auraProbability
		end
	end
	wait(0.01)
end

This part of code was generated by AI

why are you disappointed with your code? is it broken? if it works it works. but if you are trying to understand the code maybe don’t rely on AI. did you ever try asking it what each part of the code really does?

bro is using ai to reply to posts :skull:

this is work and I’m understand it, I’m just think it could be better, but i don’t know how to make it better

instead of repeatedly using game:GetService("ReplicatedStorage") in every single variable you should just define ReplicatedStorage in a single variable. less words

are you trying to wait as fast as you can? or do you just want it to wait every 0.01 seconds? you can simply use task.wait() without numbers and it will wait as fast as it can, without crashing the game.

Well, maybe i should send a .rblx file with this code?