Problem with difficulty script on an obby game

  1. I’m trying to fix my difficulty script that shows the players difficulty, on an obby game.

  2. The difficulty script won’t work. It doesn’t show any errors, and I already tried printing stuff.
    image (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

Server Script In SS Service
local module = require(game.ReplicatedStorage.Functions)

game.Players.PlayerAdded:Connect(function(Player)
	Player:WaitForChild("leaderstats").Stage.Changed:Connect(function()
		module:UpdateStageColor(Player)
	end)
	Player.CharacterAdded:Connect(function()
		module:UpdateStageColor(Player)
	end)
end)

  • Basically I’m trying to fix my difficulty updater script, but I don’t see where the problem is.

(Thank you for any help!!)

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).

As a localscript inside your gui

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)

Test.rbxl (26.3 KB)

1 Like

It works! Are you able to add CharacterAdded too?

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)
2 Likes

Thanks. It Works!⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀