Hello, im having problems with my code
-
What do you want to achieve? A remote that passes a chosen rarity, duplicates that and shows on the board.
-
What is the issue? Unable to cast value to object error
-
What solutions have you tried so far? ChatGPT
This is server code
local Remotes = game:GetService("ReplicatedStorage").Remotes
local runeModule = require(game:GetService("ReplicatedStorage").Modules.RuneConfig)
Remotes.RollRune.OnServerEvent:Connect(function(plr, runeTitle)
getRandomRarity(plr, runeTitle)
end)
function compare(a, b)
return a > b
end
function getRandomRarity(plr: Player, runeTitle)
local runeList = runeModule[runeTitle]
local weights = {}
for runeName, runeData in pairs(runeList) do
local runeRarity = runeData.Rarity
table.insert(weights, runeRarity)
end
table.sort(weights, compare)
local total_weights = 0
for _, weight in ipairs(weights) do
total_weights = total_weights + weight
end
local randomWeight = math.random(1, total_weights)
local cumulativeWeight = 0
for runeName, runeData in pairs(runeList) do
cumulativeWeight = cumulativeWeight + runeData.Rarity
if randomWeight <= cumulativeWeight then
plr.multi.Value -= 10
plr.tm.Value += runeData.TMGain
plr.mm.Value += runeData.MMGain
plr.rm.Value += runeData.MMGain
local selectedRarity = tostring(runeData.Name)
Remotes.DisplayRecentRoll:FireClient(selectedRarity)
return
end
end
end
And this is client code
local remotes = game:GetService("ReplicatedStorage").Remotes
local debounce = false
local plr = game.Players.LocalPlayer
local runeBoard = workspace.RuneBoard
local runeSurface = runeBoard:WaitForChild("Board").SurfaceGui
local recentRunes = runeSurface.RunesHolder.RecentRunes
local rollRunes = runeSurface.RunesHolder.RuneMain
local rollButton = rollRunes.RollButton
rollButton.MouseButton1Click:Connect(function()
if debounce == false then
debounce = true
remotes.RollRune:FireServer("BasicRune")
task.wait(0.1)
debounce = false
end
end)
remotes.DisplayRecentRoll.OnClientEvent:Connect(function(rarity: string)
local runeLabelClone = script:FindFirstChild(rarity)
if runeLabelClone then
print("Found Object:", rarity)
runeLabelClone = runeLabelClone:Clone()
runeLabelClone.Parent = recentRunes.RecentRunesList
task.wait(4)
runeLabelClone:Destroy()
else
print("Error: Object with name '" .. rarity .. "' not found.")
end
end)
Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.