I’m trying to fix my difficulty script that shows the players difficulty, on an obby game.
The difficulty script won’t work. It doesn’t show any errors, and I already tried printing stuff.
(Currently on Medium, but won’t change.)
Here are the scripts to make it easier:
ModuleScript In RepStorage
local module = {}
function module:UpdateStageColor(Player)
if Player:WaitForChild("leaderstats").Stage.Value <= 6 then
Player:WaitForChild("PlayerGui"):WaitForChild("Main").Difficulty.Difficulty.ImageColor3 = Color3.fromRGB(0,255,0)
Player:WaitForChild("PlayerGui"):WaitForChild("Main").Difficulty.DifficultyText.Text = "Easy"
else
if Player:WaitForChild("leaderstats").Stage.Value >= 7 then
Player:WaitForChild("PlayerGui"):WaitForChild("Main").Difficulty.Difficulty.ImageColor3 = Color3.fromRGB(255,255,0)
Player:WaitForChild("PlayerGui"):WaitForChild("Main").Difficulty.DifficultyText.Text = "Medium"
end
end
end
return module
You should definitely shorten your code by adding local variables, they are also faster to access the second time (and further) since it will be stored.
For the issue, try adding print() provided with a string/number to see where your code stops.
This helps us (and yourself to see where the code stops).
local Difficulties = {
[1] = {
"Easy",
Color3.fromRGB(0, 255, 0)
},
[7] = {
"Medium",
Color3.fromRGB(255, 143, 0)
},
[12] = {
"Hard",
Color3.fromRGB(143, 0, 0)
},
}
local DifficultyImage = script.Parent.ImageLabel
local DifficultyLabel = script.Parent.TextLabel
local Player = game.Players.LocalPlayer
local Leaderstats = Player:WaitForChild("leaderstats")
local Stage = Leaderstats:WaitForChild("Stage")
Stage:GetPropertyChangedSignal("Value"):Connect(function()
local Num = nil
for i,v in pairs(Difficulties) do
if Num == nil then Num = i continue end
if Num < i then
Num = i
end
end
for i = Num, 1, -1 do
if Difficulties[i] == nil then continue end
if Stage.Value >= i then
DifficultyLabel.Text = Difficulties[i][1]
DifficultyImage.ImageColor3 = Difficulties[i][2]
return
end
end
DifficultyLabel.Text = Difficulties[1][1]
DifficultyImage.ImageColor3 = Difficulties[1][2]
end)
local Difficulties = {
[1] = {
"Easy",
Color3.fromRGB(0, 255, 0)
},
[7] = {
"Medium",
Color3.fromRGB(255, 143, 0)
},
[12] = {
"Hard",
Color3.fromRGB(143, 0, 0)
},
}
local DifficultyImage = script.Parent.ImageLabel
local DifficultyLabel = script.Parent.TextLabel
local Player = game.Players.LocalPlayer
local Leaderstats = Player:WaitForChild("leaderstats")
local Stage = Leaderstats:WaitForChild("Stage")
local function Update()
local Num = nil
for i,v in pairs(Difficulties) do
if Num == nil then Num = i continue end
if Num < i then
Num = i
end
end
for i = Num, 1, -1 do
if Difficulties[i] == nil then continue end
if Stage.Value >= i then
DifficultyLabel.Text = Difficulties[i][1]
DifficultyImage.ImageColor3 = Difficulties[i][2]
return
end
end
DifficultyLabel.Text = Difficulties[1][1]
DifficultyImage.ImageColor3 = Difficulties[1][2]
end
Stage:GetPropertyChangedSignal("Value"):Connect(function()
Update()
end)
Player.CharacterAdded:Connect(function()
Update()
end)