Script not working

Hello!
So, I’m making a game, like a maze game where there are some guards and you need to stay out of their vision. When they walk, they create parts. Their name are “Waypoints”, and I made a special model for them. By the way, they are invisible (transparency 1). I want to make a gamepass that shows them (transparency 0). I made a script with :GetChildren() but it didn’t work. The script needs to find all the parts, and make them visible for those who have the gamepass. Note: The parts are being created every second so they aren’t pre-made by me. I tried to make it work like 2-3 times but I’ve failed. Here’s the code:

local WORKSPACE_PARTS = workspace:WaitForChild("WaypointsModel"):GetChildren()
local id = 6642754

game.Players.PlayerAdded:Connect(function(player)
    if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player, id) then 
	   for i, waypoint in ipairs(WORKSPACE_PARTS) do
	      waypoint.Transparency = 0
	   end
    end
end)

Correction


MarketplaceService | Documentation - Roblox Creator Hub uhh what, system is broken

This uses userId as the first argument. Consider changing it to player.UserId instead.

Thanks for the correction, though, the rest of the code doesn’t work.

The funky thing is that if you want the waypoints to be visible for only players with the game pass, you’ll have to make sure that the code is ran through client(LocalScript).

In other words, if the code is server-sided, all players will see the same replication(if a player with the game pass joins, the points become visible for all players). Otherwise, if the code is client-sided but it’s not working, it’s probably incorrectly parented.

1 Like

If the player is added before the waypoints exist, then nothing will be updated. Put this code in a local script.
Try the following code:

local player = game.Players.LocalPlayer
local WORKSPACE_PARTS = workspace:WaitForChild("WaypointsModel")
local id = 6642754

    if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, id) then
while true do
	   for i, waypoint in ipairs(WORKSPACE_PARTS:GetChildren()) do
	      waypoint.Transparency = 0
	   end
wait(1)
end
    end
1 Like

Not working, but thanks though.

Please do not just say “Not Working”. Are there any errors, are parts existing? This code worked perfectly for me with a demo gamepads, you are doing something wrong.

Not sure why you would use a while loop, couldn’t you use ChildAdded?

There are no errors in the output.

It only runs every second. Also, setting values to the current values has no effect. While it may be slightly better practice, I prefer to do it this way. Both ways would work.

Are you sure there are parts in the waypoints model? The code I made works perfectly for me. Is it in a local script? What is the object heirarchy?

Yes, the parts are in the waypoints model and it is a local script.

Alright, I put the script in “StarterPlayerScripts” and it works now. Thank you.

Where did you have it before? LocalScripts can’t run on the server. (I’m just curious)

I have put the local script in the workspace :sweat_smile:

Yeah, local scripts have to be a descendant of a player instance to work.

Glad that it works now but I’m a little confused as to why you need to loop through it every second, and also for this you don’t need to use ipairs.

local player = game.Players.LocalPlayer
local WORKSPACE_PARTS = workspace:WaitForChild("WaypointsModel")
local id = 6642754

if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, id) then
    for _, waypoint in pairs(WORKSPACE_PARTS:GetChildren()) do
        waypoint.Transparency = 0
    end
end
2 Likes

Thank you for your time too! Sorry if I had bothered you! :wink: