MVP Script, Guidance

Hello, I’ve recently tried to make an MVP Script that will give the player an MVP Text above his head if he has more than 1000 points.

The script:

local plr = game:GetService("Players").LocalPlayer
local leaderstatscript = script.Parent.leaderstats
local leaderstats = plr:WaitForChild("leaderstats")
----
if leaderstats.Value <= 1000 then do
		game.Workspace.Part.SurfaceGui.TextLabel.Text = leaderstats.Value
		game.Workspace.Part.SurfaceGui.TextLabel.TextColor3 = Color3.new(0, 0, 0) --change color to the prefered color
		print(plr.Name..leaderstats.Value)
		
		local Humanoid = plr:FindFirstChild("Humanoid")
		Instance.new("BillboardGui", Humanoid)
		Humanoid.BillboardGui.Text = "MVP"
		Humanoid.BillboardGui.Text.TextColor3 = Color3.new(0, 0, 0)  --change color to the prefered color
	end
end

I might have did it wrong or my code is not good for this feature but I hope someone will help me eventually. :smile:

1 Like

There are a few issues with this:

  • Greater than or equal to is >=, not <=
  • You need to have an IntValue, you can’t just change it directly from the ‘leaderstats’
  • The BillboardGui should preferably be parented to the player’s head
local plr = game:GetService("Players").LocalPlayer
local leaderstatscript = script.Parent.leaderstats
local leaderstats = plr:WaitForChild("leaderstats").Points --// change to your points value
----
if leaderstats.Value >= 1000 then do
		game.Workspace.Part.SurfaceGui.TextLabel.Text = leaderstats.Value
		game.Workspace.Part.SurfaceGui.TextLabel.TextColor3 = Color3.new(0, 0, 0) --change color to the prefered color
		print(plr.Name..leaderstats.Value)
		
		local Humanoid = plr:FindFirstChild("Humanoid")
		Instance.new("BillboardGui", Humanoid)
		Humanoid.BillboardGui.Text = "MVP"
		Humanoid.BillboardGui.Text.TextColor3 = Color3.new(0, 0, 0)  --change color to the prefered color
	end
end
1 Like

Soo uhhh, I’ve ran into an problem:

image

I tried redefining the plr variable from:

local plr = game:GetService("Players").LocalPlayer

--// to

local plr = game.Players.LocalPlayer

but other than that Idk what I can do…

That shouldn’t be an issue. Can you point out what line the error is on in your script?

well you can’t access a LocalPlayer from a server script, but there might be another issue

1 Like

Its on the line 3 the error:

My bad for not noticing that.
Yeah, you can’t get the LocalPlayer from the server scripts (only from LocalScripts).

One solution would be:

  • Firing an event from the client to the server when the player should get the MVP badge, do the handling on the client
1 Like

BillboardGui isn’t a TextLabel, also using Instance.new with the parent is bad practice, see this.

Besides, you cannot access the LocalPlayer from the server-side.
You should instead use the PlayerAdded event that is in the Players service.

Your current script would also only check it once, so if a player reached 1000 points they’d have to rejoin.

Why are you defining leaderstatsScript when you are not using it anywhere?

Another problem I see is that you’re trying to get the leaderstats value, which wouldn’t be actually shown on the Roblox leaderboard.
You probably meant to reference points instead of leaderstats.

Also, the humanoid isn’t a part of the player, rather the player’s character.

I’m not sure if BillboardGui would show if it was parented to a humanoid.

Your fixed code would probably look more like this:

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local MVP_MIN_POINTS = 1000

local function CreateMVPBillboard(Parent)
	local BillboardGui = Instance.new("BillboardGui")
	BillboardGui.Name = "MVP BillboardGui"
	BillboardGui.Adornee = Parent
	BillboardGui.LightInfluence = 0
	BillboardGui.Size = UDim2.new(4, 0, 4, 0)
	BillboardGui.StudsOffsetWorldSpace = Vector3.new(0, 4, 0)
	BillboardGui.ZIndexBehavior = Enum.ZIndexBehavior.Global

	local TextLabel = Instance.new("TextLabel")
	TextLabel.BackgroundTransparency = 1
	TextLabel.Name = "MVP Label"
	TextLabel.Text = "MVP"
	TextLabel.TextColor3 = Color3.fromRGB(255, 50, 50)
	TextLabel.Size = UDim2.new(0.8, 0, 0.8, 0)
	-- helps to distinct text in any environment
	TextLabel.TextStrokeTransparency = 0
	TextLabel.TextScaled = true
	TextLabel.Parent = BillboardGui

	BillboardGui.Parent = Parent
	return BillboardGui
end

local function PlayerAdded(Player)
	local leaderstats = Player:WaitForChild("leaderstats")
	local Points = leaderstats:WaitForChild("Points")
	Player.CharacterAdded:Connect(function(Character)
		repeat RunService.Heartbeat:Wait() until workspace:IsAncestorOf(Character) 

		local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
		local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
		if not HumanoidRootPart then
			return
		end

		if not Humanoid then
			return
		end

		local BillboardGui
		if MVP_MIN_POINTS <= Points.Value then
			BillboardGui = CreateMVPBillboard(HumanoidRootPart)
		end

		local c
		c = Points:GetPropertyChangedSignal("Value"):Connect(function()
			if MVP_MIN_POINTS > Points.Value then
				if BillboardGui then
					BillboardGui:Destroy()
					BillboardGui = nil
				end
			elseif MVP_MIN_POINTS <= Points.Value then
				if not BillboardGui then
					BillboardGui = CreateMVPBillboard(HumanoidRootPart)
				end
			end
		end)

		-- I don't need to disconnect the died connection, due to the fact that
		-- the connection gets erased once the Humanoid is destroyed,
		-- which is probably a few seconds after .Died is fired.
		Humanoid.Died:Connect(function()
			c:Disconnect()
			if BillboardGui then
				BillboardGui:Destroy()
				BillboardGui = nil
			end
		end)
	end)
end
Players.PlayerAdded:Connect(PlayerAdded)

This should be in a server script.