Spawner is not being restricted

Hello. I was adding gamepass/badge restrictions to a vehicle spawner.
I tried to test it, but it doesn’t detect if player owns badge/pass.
Script:

local config = script.Parent
local pass = config.IsGamePass.ID.Value
local badge = config.IsBadge.ID.Value
local button = script.Parent.Parent:FindFirstChild("Button")

local ms = game:GetService("MarketplaceService")
local badges = game:GetService("BadgeService")

game.Players.PlayerAdded:Connect(function(add)
	local plr = game.Players:GetPlayerFromCharacter(add)
	if config.IsGamePass.Value == true and ms:UserOwnsGamePassAsync(plr.UserId, pass) then
		button.Changed:Connect(function()
			if button.Color == Color3.new(0.0352941, 0.537255, 0.811765) then
				button.Color = Color3.new(0.784314, 0, 1)
			end
		end)
	elseif config.IsBadge.Value == true and badges:UserHasBadgeAsync(plr.UserId, badge) then
		button.Changed:Connect(function()
			if button.Color == Color3.new(0.0352941, 0.537255, 0.811765) then
				button.Color = Color3.new(1, 0.615686, 0)
			end
		end)
	else
		for _, scripts in pairs(script.Parent.Parent:GetDescendants()) do
			if scripts:IsA("Script") and scripts.RunContext == "Legacy" then
				script.Parent.Parent.Button.Color = Color3.new(0.196078, 0.196078, 0.196078)
				script.Parent.Parent.Button.VehicleLocked.Transparency = 0
				scripts.Enabled = false
			end
		end
	end
end)

Explorer: (the localscript is actually a serverscript with runcontext set to client)
image

1 Like

I think the issue is that the player being defined is the player’s character. Use the player for the functions get:async instead.

try changing this:

game.Players.PlayerAdded:Connect(function(plr)
--remove this line -->local plr = game.Players:GetPlayerFromCharacter(add)
--rest of the code
1 Like

The button is still not restricted, after deleting that part.

Interesting. I also changed the “add” parameter in the player added function to plr, incase you missed that. Are any errors being thrown?

No errors are showing in the output.

perhaps it would be better to change just the colours in the client script, and check if they own the gamepass / badge in the “activated” script, instead of using the for loop.

Ok I added some prints.
Seems like it stops at PlayerAdded.

I think it is because your own client isnt detecting when you get added, so you should probably seperate the script into a server script and a client script.

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