Script is not detecting .changed, nor disabling the scripts

Hello.
I have a script that can prevent the button from being pressed if the player doesn’t own the specified gamepass/badge.
Most of the script works (i tried fixing it), however the script doesn’t disable the other scripts, or detect .Changed.
Script:

local config = script.Parent
local pass = config.IsGamePass.ID
local badge = config.IsBadge.ID
local button = script.Parent.Parent:WaitForChild("Button")
if button then
	print("button found")
end

local script1 = script.Parent.Parent:WaitForChild("SpawnVehicle")
local script2 = button:WaitForChild("Activate")

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

local function handlePlayer(Player : Player)
	print("plr added")
	if config.IsGamePass.Value == true then	
		if ms:UserOwnsGamePassAsync(Player.UserId, pass.Value) then
			button.Color = Color3.new(0.784314, 0, 1)
			print("Player has pass")
			if button then
				button.Changed:Connect(function() -- not working
					if button.Color == Color3.new(0.0352941, 0.537255, 0.811765) then
						button.Color = Color3.new(0.784314, 0, 1)
						print("button color has changed")
					end
				end)
			end
		else
			print("Player does not own the car")
			script.Parent.Parent.Button.Color = Color3.new(0.196078, 0.196078, 0.196078)
			script.Parent.Parent.Button.VehicleLocked.Transparency = 0
			task.wait(2)
			for _, scripts in pairs(script.Parent.Parent:GetDescendants()) do
				if scripts:IsA("Script") and scripts.RunContext == "Legacy" then
					scripts.Enabled = false
				end
			end
		end
	elseif config.IsBadge.Value == true then
		if badges:UserHasBadgeAsync(Player.UserId, badge.Value) then
			button.Color = Color3.new(1, 0.615686, 0)
			button.Changed:Connect(function() -- not working
				if button.Color == Color3.new(0.0352941, 0.537255, 0.811765) then
					button.Color = Color3.new(1, 0.615686, 0)
				end
			end)
		else
			print("Player does not own the car")
			script.Parent.Parent.Button.Color = Color3.new(0.196078, 0.196078, 0.196078)
			script.Parent.Parent.Button.VehicleLocked.Transparency = 0
			script1.Enabled = false	-- not working
			script2.Enabled = false -- not working
			print("Spawner disabled")
		end
	end
end

for _, Player in pairs(game.Players:GetPlayers()) do
	handlePlayer(Player);
end

game.Players.PlayerAdded:Connect(handlePlayer);

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

1 Like

instead of disabling scripts, just do a check inside of the scripts you want to disable to see if they own the gamepass

Won’t that affect the server?
(most scripts in the spawner is serversided)

well if you are just viewing the value then no it wont effect anything.

Updated reply

local config = script.Parent
local pass = config.IsGamePass.ID
local badge = config.IsBadge.ID
local button = script.Parent.Parent:WaitForChild("Button")
local script1 = script.Parent.Parent:WaitForChild("SpawnVehicle")
local script2 = button:WaitForChild("Activate")

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

local function disableScripts()
    for _, scripts in pairs(script.Parent.Parent:GetDescendants()) do
        if scripts:IsA("Script") then
            scripts.Enabled = false
        end
    end
end

local function handleButtonChange()
    if button.Color == Color3.new(0.0352941, 0.537255, 0.811765) then
        if config.IsGamePass.Value then
            button.Color = Color3.new(0.784314, 0, 1)
        elseif config.IsBadge.Value then
            button.Color = Color3.new(1, 0.615686, 0)
        end
    end
end

local function handlePlayer(player)
    local hasGamePass = config.IsGamePass.Value and ms:UserOwnsGamePassAsync(player.UserId, pass.Value)
    local hasBadge = config.IsBadge.Value and badges:UserHasBadgeAsync(player.UserId, badge.Value)

    if hasGamePass or hasBadge then
        if hasGamePass then
            button.Color = Color3.new(0.784314, 0, 1)
        else
            button.Color = Color3.new(1, 0.615686, 0)
        end

        button.Changed:Connect(handleButtonChange)
    else
        button.Color = Color3.new(0.196078, 0.196078, 0.196078)
        script.Parent.Parent.Button.VehicleLocked.Transparency = 0
        task.wait(2)
        disableScripts()
    end
end

for _, player in pairs(game.Players:GetPlayers()) do
    handlePlayer(player)
end

game.Players.PlayerAdded:Connect(handlePlayer)

Doesn’t work, same results.

aa

Time to put out the prints and start tracking down where the problem is …
Start stripping off the extras down to the core of what it’s doing if you have to.

Here are the problems:

for _, scripts in pairs(script.Parent.Parent:GetDescendants()) do
	if scripts:IsA("Script") then
		script.Enabled = false
		print("scripts disabled")
	end
end
local function handleButtonChange()
	if button.Color == Color3.new(0.0352941, 0.537255, 0.811765) then
		if config.IsGamePass.Value then
			button.Color = Color3.new(0.784314, 0, 1)
			print("button changed")
		elseif config.IsBadge.Value then
			button.Color = Color3.new(1, 0.615686, 0)
			print("button changed")
		end
	end
end

I cloned the button, tested it out with just these parts. Neither of them works.
UPDATE: found out .changed does not work on client-sided scripts, any workarounds to this?

This is a simple matter of calling a remote …

ServerScript

Location: ServerScriptService

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("UpdateButtonColor")

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

local config = script.Parent
local passID = config.IsGamePass.ID
local badgeID = config.IsBadge.ID

local function checkPlayer(player)
	local hasGamePass = config.IsGamePass.Value and MarketplaceService:UserOwnsGamePassAsync(player.UserId, passID)
	local hasBadge = config.IsBadge.Value and BadgeService:UserHasBadgeAsync(player.UserId, badgeID)

	if hasGamePass or hasBadge then
		if hasGamePass then
			remoteEvent:FireClient(player, Color3.new(0.784314, 0, 1))
		else remoteEvent:FireClient(player, Color3.new(1, 0.615686, 0))
		end
	else remoteEvent:FireClient(player, Color3.new(0.196078, 0.196078, 0.196078))
	end
end

Players.PlayerAdded:Connect(checkPlayer)
ClientScript

Location: StarterPlayerScripts or StarterGui

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("UpdateButtonColor")

local button = script.Parent.Parent:WaitForChild("Button")

local function updateButtonColor(color)
    button.Color = color
end

remoteEvent.OnClientEvent:Connect(updateButtonColor)

You will need to place a remote here …
local remoteEvent = ReplicatedStorage:WaitForChild(“UpdateButtonColor”)

And yes I posted an error last time … :man_facepalming:
Updated.

instead of .changed you can use :GetPropertyChangedSignal("Property")
This function triggers when ever a property is changed

I also tried that. It didn’t work too.

2 Likes

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