Why does this not work?

Why does thsi script not work?

local player = game.Players.LocalPlayer
local BadgeService = game:GetService("BadgeService")

if BadgeService:UserHasBadgeAsync(player.UserId, 1111111) then
	workspace.rooms.Basement.Donotgobeyond.SurfaceGui.TextLabel.Text = "u already have the badge lol"
	wait(3)
	workspace.W1.W2.WTEXT.SurfaceGui.TextLabel.Text = "the default text"
end

if not BadgeService:UserHasBadgeAsync(player.UserId, 1111111) then
	game.Workspace.W.W1.W2.open.Enabled = true
	wait(3)
	game.Workspace.W.W1.W2.open.Enabled = false
end

Practically everything is a template to avoid leaking.

3 Likes

Please explain what is not functioning in this code…? I cannot really help you much unless I know what’s the issue.

1 Like

The text doesn’t change to “u already have the badge lol”

1 Like

Nvm, here’s the error:
workspace.W1.W2.W3.badgelocked:4: attempt to index nil with ‘UserID’

1 Like

It seems to be a server script then, Players.LocalPlayer is only present on the client. Consider moving the script to the client.

1 Like

Is there a way to change Players.LocalPlayer to something else that makes it possible for it to be a serverscript?

1 Like

A server script replicates property changes to all clients, so it wouldn’t function.

1 Like

It doesn’t even do anything. It doesn’t change the text even when changed to Client script.
image

Is there no output in the console?

Nuh uh, none at all.

Can you explain how can a player activate those messages? It appears they are executed once, and never again.

you can with playeradded event

game.Players.Playeradded:Connect(function(player)
local BadgeService = game:GetService("BadgeService")

if BadgeService:UserHasBadgeAsync(player.UserId, 1111111) then
	workspace.rooms.Basement.Donotgobeyond.SurfaceGui.TextLabel.Text = "u already have the badge lol"
	wait(3)
	workspace.W1.W2.WTEXT.SurfaceGui.TextLabel.Text = "the default text"
end

if not BadgeService:UserHasBadgeAsync(player.UserId, 1111111) then
	game.Workspace.W.W1.W2.open.Enabled = true
	wait(3)
	game.Workspace.W.W1.W2.open.Enabled = false
end
end)

yeah use the event players.playeradded on a server script

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	print(player.UserId.. " joined the game!")
end)

Players.PlayerRemoving:Connect(function(player)
	print(player.Name .. " left the game!")
end)

They touch the part, and then if they have the badge then it says you can’t enter

Why PlayerAdded? i don’t need playeradded

Use the desired Part’s Touched event. Make sure that whatever hits the Part actually came from the Player’s character. Make a debounce so that it doesn’t flicker.

local player = game.Players.LocalPlayer
local BadgeService = game:GetService("BadgeService")

local debounce = false

Type_The_Path_Of_The_Part_That_Will_Trigger_This_Function.Touched:Connect(function(Hit : BasePart)
	if debounce then return end
	
	local model = Hit:FindFirstChildWhichIsA("Model")
	
	-- Check if the part that touched the (door?) is part of the player's character.
	if not model or not (player.Character == model) then
		return
	end
	
	debounce = true
	
	if BadgeService:UserHasBadgeAsync(player.UserId, 1111111) then
		workspace.rooms.Basement.Donotgobeyond.SurfaceGui.TextLabel.Text = "u already have the badge lol"
		wait(3)
		workspace.W1.W2.WTEXT.SurfaceGui.TextLabel.Text = "the default text"
	else
		game.Workspace.W.W1.W2.open.Enabled = true
		wait(3)
		game.Workspace.W.W1.W2.open.Enabled = false
	end
	
	debounce = false
end)