Trying to make a highlight npc gamepass

i want to make a gamepass which when bought npcs become highlighted
however it doesnt work and i dont know why

ive tried chatgpt and youtube tutorials but nothing is working

if you have any ideas on what to do let me know

3 Likes

in starterplayerscripts i have the code

– HighlightNPCsScript in StarterPlayerScripts

local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local Players = game:GetService(“Players”)

local player = Players.LocalPlayer

– Function to check gamepass ownership
local function playerOwnsGamepass()
local checkGamepass = ReplicatedStorage:WaitForChild(“CheckGamepass”)
local hasPass = checkGamepass:InvokeServer()
return hasPass
end

– Function to highlight NPCs
local function highlightNPCs()
local npcsFolder = game.Workspace:FindFirstChild(“NPCs”)
if npcsFolder then
for _, npc in pairs(npcsFolder:GetChildren()) do
if npc:IsA(“Model”) then
for _, part in pairs(npc:GetChildren()) do
if part:IsA(“BasePart”) then
local highlight = Instance.new(“SelectionBox”)
highlight.Adornee = part
highlight.Parent = part
highlight.LineThickness = 0.1
highlight.Color3 = Color3.fromRGB(255, 0, 0) – Highlight color
highlight.Transparency = 0.5
end
end
end
end
else
warn(“No NPCs folder found in Workspace”)
end
end

– Check if the player owns the gamepass and highlight NPCs if they do
if playerOwnsGamepass() then
highlightNPCs()
else
warn(“Player does not own the gamepass”)
end

in servecterscript service i have the code

– GamepassChecker script in ServerScriptService

local gamepassId = 870314415 – Replace with your actual Gamepass ID
local Players = game:GetService(“Players”)
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)

– Remote event to check gamepass ownership
local checkGamepassEvent = ReplicatedStorage:WaitForChild(“CheckGamepassEvent”)

checkGamepassEvent.OnServerEvent:Connect(function(player)
local success, hasPass = pcall(function()
return player:HasPass(gamepassId)
end)
if success then
checkGamepassEvent:FireClient(player, hasPass)
else
warn("Error checking gamepass ownership: " … tostring(hasPass))
checkGamepassEvent:FireClient(player, false)
end
end)

i have a remote event in replicated storage called CheckGamepassEvent

and a folder named NPCs with my npc in

1 Like

player:HasPass() is now deprecated, and now longer works for new gamepasses. Try MarketPlaceSevice:UserHasGamePassAsync() instead.

1 Like

if it says haspass should i change it to MarketPlaceSevice?

1 Like

Change the entirety of player:HasPass() to game:GetService("MarketplaceService"):UserHasGamePassAsync() (as shown below).

local MarketplaceService = game:GetService("MarketplaceService"

local success, errorMessage = pcall(function()
    return MarketplaceService:UserHasGamePassAsync(player.UserId, gampassId)
end

if success then
    -- do highlight npcs code here
end

so would it look like this?

Yes, but make sure to adjust it to your needs, as what I just showed was a general example of how to use it.

(Also, my mistake, add a parenthesis after “MarketplaceService”, I forgot to add that.)

its not working but would that be because of the highlighted errors in the code (haspass, player, gamepassid end and local)

could it also be to do with this code?

still cant find the solution, any ideas?

Try replacing your server script with this.

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local MarketplaceService = game:GetService("MarketplaceService")

checkGamepassEvent = ReplicatedStorage:WaitForChild("CheckGamepassEvent")

local gamepassId = 870314415

checkGamepassEvent.OnServerInvoke = function(player)
	return MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamepassId)
end

(Make sure checkGamepassEvent is a RemoteFunction!)

sorry what do you mean by remote function? (im still learning lua btw) is that the same as a remote event? also could the problem be to do with my npc?

A RemoteFunction is slightly different from a RemoteEvent. Basically, a RemoteFunction is a way to communicate across client-side boundaries, whereas a RemoteEvent can only transfer information, without receiving a result in return. More information here if it helps: RemoteFunction | Documentation - Roblox Creator Hub

From what I see, you’re npcs should be fine, only because of the little information I have currently.

thank you for explaining, and if you would like any information of the scripts or npc or anything let me know

It would be helpful if you sent a picture of your npc folder with your npcs in explorer, just to confirm it matches with your code.

Also, did the piece of code I sent you work on post #11? Just curious, sorry if I sound impatient.

sorry iT didnt work im not sure why but heres the images if this is what you mean
image
image

image

image

1 Like

It seems like checkGamepassEvent is still a RemoteEvent. Try changing it to a RemoteFunction, then try again.

sorry i clicked event instead of function but its somewhat worked when i changed it! it now looks like this
image
which is different as before apsolutely nothing happened