How do I fix this script?

So I have this server script inside of a part (part also has a ClickDetector inside of it), and I keep getting this error:

Script:

game:WaitForChild("Players").PlayerAdded:Connect(function(player)
	
end)
local Button = game.Workspace.PortalArea.Light1
local ClickDetector = script.Parent

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

ClickDetector.MouseClick:Connect(function()
	
	if BadgeService:UserHasBadgeAsync(player.UserId, BadgeId) then
		Button.Color = Color3.new(0, 255, 0)
	end

end)
1 Like

First of all, you already define the player

Second of all Local Player is only used in Local Scripts

1 Like
local Players = game:GetService("Players")
local BadgeService = game:GetService("BadgeService")

local Button = game.Workspace.PortalArea:WaitForChild("Light1")
local ClickDetector = script.Parent

ClickDetector.MouseClick:Connect(function()
    local player = Players.LocalPlayer

    if player then
        local userId = player.UserId

        if BadgeService:UserHasBadgeAsync(userId, 2146480330) then
            Button.Color = Color3.new(0, 255, 0)
        end
    end
end)

Try this, place the script inside of a click detector.

1 Like

you cant use localplayer in a serverscript because the server has no idea which player you are (also make sure to set the BadgeId to a valid one)

local Button = game.Workspace.PortalArea.Light1
local ClickDetector = script.Parent

local BadgeService = game:GetService("BadgeService")
local BadgeId = 000000000

ClickDetector.MouseClick:Connect(function(Player)
	
	if BadgeService:UserHasBadgeAsync(Player.UserId, BadgeId) then
		Button.Color = Color3.new(0, 255, 0)
	end

end)
2 Likes

For some reason the part isn’t changing colors, and there is nothing showing in output

Color3.new uses value from 0-1 what you should use is

		Button.Color = Color3.fromRGB(0, 255, 0)

1 Like

Seem’s to be working, thank you so much!!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.