You should put all those if statements in a GetPropertyChangedSignal(“Value”) event so that when it changes those if statements will fire. Them on their own fire once the server initiates but that’s it.
local Rarity = script.Parent.Parent:WaitForChild("Rarity")
Rarity.Changed:Connect(function(val)
if val== "Common" then
script.Parent.BackgroundColor = Color3.fromRGB(169, 169, 169)
elseif val == "Uncommon" then
script.Parent.BackgroundColor = Color3.fromRGB(50, 205, 50)
elseif val == "Rare" then
script.Parent.BackgroundColor = Color3.fromRGB(30, 144, 255)
elseif val == "Epic" then
script.Parent.BackgroundColor = Color3.fromRGB(153, 51, 255)
elseif val == "Legendary" then
script.Parent.BackgroundColor = Color3.fromRGB(255, 140, 0)
elseif val == "Mythic" then
script.Parent.BackgroundColor = Color3.fromRGB(255, 0, 0)
end
end)
How are the parts created? Are they cloned with a server script into the workspace or anything like that? If yes, change it to a ServerScript (you could change it to a ServerScript anyways, it’s safer).
And make sure that the value is set correctly, so no lowercase/uppercase change or extra letter or anything.
And you can also put a print into every line to check if the listener even runs
The parts are created locally actually, it gets pos, generates a part in that pos. Here’s that script btw:
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local RS = game.ReplicatedStorage
local RR = RS.RandomRarity
Mouse.Button1Down:Connect(function()
RR:FireServer()
local Chest = Instance.new("Part", workspace)
Chest.Name = "SpawnedChest"
local MousePos = Mouse.Hit.p
Chest.Position = MousePos
local CD = Instance.new("ClickDetector", Chest)
CD.RightMouseClick:Connect(function()
local RS = game.ReplicatedStorage
local BG = RS.BillboardGui
local CC = BG:Clone()
CC.Parent = Chest
end)
end)
Then it leaves it off to the server script to choose a rarity like this:
local RS = game.ReplicatedStorage
local BG = RS.BillboardGui
local Rarity = BG.Rarity
local multipliersArray = {
Common = 0.50,
Uncommon = 0.35,
Rare = 0.10,
Epic = 0.05,
Legendary = 0.01,
Mythic = 0.001
}
local function chooseIndex(multipliersArray)
local weightedSum = 0
for i,v in pairs(multipliersArray) do
weightedSum += v
end
local random = Random.new()
local rnd = random:NextNumber(0, weightedSum)
for i,v in pairs(multipliersArray) do
if rnd < v then
Rarity.Value = i
break
end
rnd -= v
end
end
RS.RandomRarity.OnServerEvent:Connect(function()
chooseIndex(multipliersArray)
end)
I don’t really understand what’s happening here. If the parts are made locally, how should the server know which part’s value to set? It can’t even see it, and you don’t try to set its value, just determine what it should be and set a StringValue inside the ReplicatedStorage. You should rewrite the scripts, and make sure ALL OF THEM are ServerScripts, and that you set the correct value inside the part.
You should only have 2 (server) scripts, one that clones the part into the workspace, and one that chooses a rarity for it AND changes its colour. And of course, a LocalScript that fires when the player clicks and fires a RemoteEvent to create the part.
If you still can’t figure out how to do it, let me know.
local RS = game.ReplicatedStorage
local BG = RS.BillboardGui
local Rarity = BG.Rarity
local TextLabel = BG.TextLabel
local multipliersArray = {
Common = 0.50,
Uncommon = 0.35,
Rare = 0.10,
Epic = 0.05,
Legendary = 0.01,
Mythic = 0.001
}
local function chooseIndex(multipliersArray)
local weightedSum = 0
for i,v in pairs(multipliersArray) do
weightedSum += v
end
local random = Random.new()
local rnd = random:NextNumber(0, weightedSum)
for i,v in pairs(multipliersArray) do
if rnd < v then
Rarity.Value = i
if Rarity.Value == "Common" then
TextLabel.BackgroundColor3 = Color3.fromRGB(169, 169, 169)
elseif Rarity.Value == "Uncommon" then
TextLabel.BackgroundColor3 = Color3.fromRGB(50, 205, 50)
elseif Rarity.Value == "Rare" then
TextLabel.BackgroundColor3 = Color3.fromRGB(30, 144, 255)
elseif Rarity.Value == "Epic" then
TextLabel.BackgroundColor3 = Color3.fromRGB(153, 51, 255)
elseif Rarity.Value == "Legendary" then
TextLabel.BackgroundColor3 = Color3.fromRGB(255, 140, 0)
elseif Rarity.Value == "Mythic" then
TextLabel.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
end
break
end
rnd -= v
end
end
RS.RandomRarity.OnServerEvent:Connect(function()
chooseIndex(multipliersArray)
end)
Use a table with the colors of the rarity and then just call the color from the table instead of the annoying 50 elseif’s. It makes your code easier to read and more compact.