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

How do I make this give the player an item?

So one of my scripters created a script so the player would get an item for owning a gamepass, he must have misunderstood me since I asked if the player could get an item for owning a badge. I tried tampering with it a bit and it still doesn't work, any help? Ty

thingtoownid = 660386043 --thing to own
item = "keyblade" -- item to clone from the replicated storages
multipleitems = false -- if set to true then the format of the table below  MUST be [nameofgear = "id1", nameofgear =  "id2", ['adding a space'] = "id3"] for thing to own 
multiple = {['Springtime Basket'] = "id", goldcoil = "id", ownerbasket = "id"} -- this was made for less clutter
                                                                                       


repeat wait() until script.Parent.Parent.Backpack
plr = script.Parent.Parent
if multipleitems == false then


items = game.ReplicatedStorage:FindFirstChild(item)
if items and type(thingtoownid) == "number" then
	
	if not script.Parent.Parent.StarterGear:FindFirstChild(item) and game:GetService("BadgeService"):UserHasBadge(plr,thingtoownid) then
	s=items:Clone()
	s.Parent = script.Parent.Parent.StarterGear
	sa=items:Clone()
	sa.Parent = script.Parent.Parent.Backpack
	else
		script:Remove()
	end
else
	error('No such thing as "' .. item .. '" in replicated storage!')
end
else
	
	for i,v in pairs (multiple) do
		items = game.ReplicatedStorage:FindFirstChild(tostring(i))
	if items and type(v) == "number" then
	if not script.Parent.Parent.StarterGear:FindFirstChild(tostring(i)) and game:GetService("MarketplaceService"):PlayerOwnsAsset(plr,v) then
	s=items:Clone()
	s.Parent = script.Parent.Parent.StarterGear
	sa=items:Clone()
	sa.Parent = script.Parent.Parent.Backpack
	else
		script:Remove()
	end
	
else
	error('No such thing as "' .. tostring(i) .. '" in Replicated Storage! Or gamepass ID "'.. tostring(v) ..  '" is invalid for some reason.')
	end
	end
end

2 comments

0

Just a note, I only edited the top half of the script since the bottom half is only used if the variable at the top is true.

0

What is output saying? Also where is the script located?

1 answer

Dictionary style tables

MarketplaceService/PlayerOwnsAsset

-- Put in a LocalScript in StarterPlayerScripts or StarterGui 
-- Put whatever items you're awarding in ReplicatedStorage

-- Name of the gear in ReplicatedStorage = your assetId 
local gear = {['gear1'] = 419231331, ['gear2'] = 419231331, ['gear3'] = 419231331}

while true do 
	if game.Players.LocalPlayer then 
		if game.Players.LocalPlayer.Character then 
			break
		end
	end
	wait()
end

local player = game.Players.LocalPlayer
local character = player.Character
local backpack = player:WaitForChild("Backpack")
local startergear = player:WaitForChild("StarterGear")
local replicatedStorage = game:GetService('ReplicatedStorage')
local market = game:GetService("MarketplaceService")



local function promptService(id, tool)
	if pcall(function() -- See below why I use a pcall
-- if this fails for whatever reason (id is 0, roblox goes down), it won't break the script
		if market:PlayerOwnsAsset(player, id) then 
			tool:Clone().Parent = startergear
			-- if the tool isn't in backpack, re-load it there. 
			if not backpack:findFirstChild(tool.Name) then 
				tool:Clone().Parent = backpack
			end
		else
			print(string.format("%s doesn't own the badge for [%s]", player.Name, tool.Name))
		end
	end) then 
		-- returns true to tell the function below that it worked
		return true
	else
		-- returns false to tell us PlayerOwnsAsset failed
		return false
	end
end

-- Loop through the table (dictionary style; ToolName = assetId)
for i,v in pairs(gear) do 
	local tool = replicatedStorage:findFirstChild(i)
	-- The script can't find the tool in ReplicatedStorage, you'd need to add it. 
	if not tool then print(string.format("Can't find %s in %s", i, replicatedStorage.Name)) return end 
	local alreadyAwarded = startergear:findFirstChild(tool.Name) 
	-- This will run when you respawn and already have have the weapons in your StarterGear
	if alreadyAwarded then 
		-- Checks to make sure the weapons are still in your backpack, just incase something happens
		local isUsable = backpack:findFirstChild(tool.Name) 
		-- If it isn't in your backpack, re-clone it.
		if not isUsable then 
			alreadyAwarded:Clone().Parent = backpack
		end
	else
		-- Runs the first time you join to load them into your player
		local doesPlayerOwnAsset = promptService(v, tool)
		if doesPlayerOwnAsset then 
			print("Service success")
		else
			error(string.format("Service failed for %s, the id is probably wrong", tool.Name))
		end
	end
end

1 comment

0

Thanks!

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