Little help please?

Hello, I’m trying to set this textlabel’s background color based on the rarity chosen.

local Rarity = script.Parent.Parent.Rarity

if Rarity.Value == "Common" then
	script.Parent.BackgroundColor3 = Color3.fromRGB(169, 169, 169)
elseif Rarity.Value == "Uncommon" then
	script.Parent.BackgroundColor3 = Color3.fromRGB(50, 205, 50)
elseif Rarity.Value == "Rare" then
	script.Parent.BackgroundColor3 = Color3.fromRGB(30, 144, 255)
elseif Rarity.Value == "Epic" then
	script.Parent.BackgroundColor3 = Color3.fromRGB(153, 51, 255)
elseif Rarity.Value == "Legendary" then
	script.Parent.BackgroundColor3 = Color3.fromRGB(255, 140, 0)
elseif Rarity.Value == "Mythic" then
	script.Parent.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
end

And here is what the explorer looks like:
image

The value is a StringValue, and the Value does have a rarity, but the color is not being set. How can I fix this?

1 Like

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.

Didn’t work:
image

Current code:

local Rarity = script.Parent.Parent.Rarity
Rarity:GetPropertyChangedSignal("Value"):Connect(function()
	if Rarity.Value == "Common" then
		script.Parent.BackgroundColor = Color3.fromRGB(169, 169, 169)
	elseif Rarity.Value == "Uncommon" then
		script.Parent.BackgroundColor = Color3.fromRGB(50, 205, 50)
	elseif Rarity.Value == "Rare" then
		script.Parent.BackgroundColor = Color3.fromRGB(30, 144, 255)
	elseif Rarity.Value == "Epic" then
		script.Parent.BackgroundColor = Color3.fromRGB(153, 51, 255)
	elseif Rarity.Value == "Legendary" then
		script.Parent.BackgroundColor = Color3.fromRGB(255, 140, 0)
	elseif Rarity.Value == "Mythic" then
		script.Parent.BackgroundColor = Color3.fromRGB(255, 0, 0)
	end
end)

Did I somehow mess up somewhere?

Is this being done from a local or server script. And then another question, are you changing the value locally or on the server

The script having issues is a local script, but I’m using a server script to set the value.

You could try using StringVal.Changed event

local Script = script
local TextLabel = Script.Parent
local Frame = TextLabel.Parent
local Rarity = Frame.Rarity

local Rarities = {
	Common = Color3.new(2/3, 2/3, 2/3),
	Uncommon = Color3.new(0.2, 0.8, 0.2),
	Rare = Color3.new(0.125, 0.55, 1),
	Epic = Color3.new(0.6, 0.2, 1),
	Legendary = Color3.new(1, 0.55, 0),
	Mythic = Color3.new(1, 0, 0)
}

local function OnRarityChanged(NewRarity)
	TextLabel.BackgroundColor3 = Rarities[NewRarity]
end

Rarity.Changed:Connect(OnRarityChanged)

Needs to be BackgroundColor3 not BackgroundColor.

1 Like

so Rarity.Value.Changed:Connect(function())?

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

1 Like

Problem, there is no ‘Frame’ though…

You don’t need to use a frame in every Gui. If it’s just a text label then you should be able to manipulate the background color

Oh wait, you’re right…Still keeps the bg white tho…

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.

Alright, now that the part’s spawn server-wide, should i now just change color from rarity script?

That doesn’t matter, it’s just a name for the script’s parent’s parent.

It would be the easiest to change its colour when changing is rarity, so yes.

Yeah, I saw. Should I add
local NewRarity = Rarity.Value
To your solution?

thank you, here is final code (worked):

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)

I feel as if it can be shortened. Advice?

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.

2 Likes