Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Trying to see if a player has a Asset from a MouseButton1Down?

After the "else" statement it works fine it just is not checking if the player has the asset.

It is not giving me no errors it just goes right to the else statement even though the player does have the Asset.

This is a local script in a gui.

local MarketplaceService = game:GetService("MarketplaceService")
local Main = script.Parent
local Galaxy = script.Parent.Parent:FindFirstChild('Galaxy')
local GalaxyButton = script.Parent:FindFirstChild('GalaxyButton')
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Quit = script.Parent.Parent.Galaxy:FindFirstChild('Quit')
local Buy = script.Parent.Parent.Galaxy:FindFirstChild('Buy')


GalaxyButton.MouseButton1Down:connect(function()
	local Asset = 25171036
	local hasAsset = false
	
	 local success, message = pcall(function()
        Asset = MarketplaceService:PlayerOwnsAsset(player, Asset)
	    end)
	
    if not success then
        warn("Error while checking if player has pass: " .. tostring(message))
        return
    end
	
	 if hasAsset == true then
		wait(.5)
        game.player.Backpack.Tool.Handle.Mesh.TextureId = "rbxassetid://2783168872"
	else
		Main.Visible = false
		Galaxy.Visible = true
		Quit.Visible = true
	end
end)

1 comment

0

You never change hasAsset.

1 answer

✓ Accepted answer

You have a few errors and a few things you should do instead.

You weren't actually using your FindFirstChild() methods, so use WaitForChild() instead or check them in the future.

You are not practicing DRY (Don't repeat yourself). You already defined Galaxy, you don't need to keep relocating it.

Change this

local Quit = script.Parent.Parent.Galaxy:FindFirstChild('Quit')
local Buy = script.Parent.Parent.Galaxy:FindFirstChild('Buy')

To this

local Quit = Galaxy:WaitForChild('Quit')
local Buy = Galaxy:WaitForChild('Buy')

You were doing

Asset = MarketplaceService:PlayerOwnsAsset(player, Asset) 

You should have been doing

hasAsset = MarketplaceService:PlayerOwnsAsset(player, Asset)

You were looking for a nil player when you already defined the LocalPlayer. You should also break that down into variables and make sure they're there first.

game.player.Backpack.Tool.Handle.Mesh.TextureId = "rbxassetid://2783168872"

Put it all together

local player = game.Players.LocalPlayer
-- you tried using game.player, you already defined player as a variable
local backpack = player:WaitForChild("Backpack")

local Main = script.Parent
local Galaxy = Main.Parent:WaitForChild('Galaxy')
local GalaxyButton = Main:WaitForChild('GalaxyButton')

local mouse = player:GetMouse()
local Quit = Galaxy:WaitForChild('Quit')
local Buy = Galaxy:WaitForChild('Buy')
-- You never utilized FindFirstChild() 
	-- by checking if it was there, so use WFC instead

local MarketplaceService = game:GetService("MarketplaceService")
local textureid = 'rbxassetid://2783168872'


GalaxyButton.MouseButton1Down:Connect(function()
	-- Use :Connect()
	local AssetId = 25171036
	local hasAsset = false
	
	local success, message = pcall(function()
		hasAsset = MarketplaceService:PlayerOwnsAsset(player, AssetId)
		-- You used Asset here
	end)
	
	if not success then
		warn("Error while checking if player has pass: " .. tostring(message))
	else
		if hasAsset == true then
			local tool = backpack:FindFirstChild("Tool")
			local Handle = tool and tool:FindFirstChild("Handle")
			local Mesh = Handle and Handle:FindFirstChild("Mesh")
			if Mesh then 
				Mesh.TextureId = textureid
			end
		else
			Main.Visible = false
			Galaxy.Visible = true
			Quit.Visible = true
		end
	end
end)

1 comment

1

This is very helpful. Thank you very much.

This discussion is preserved as it was. New answers, comments, and votes are closed.