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

Gears disappear when I respawn?

Hi I am Rob and I am a BEGINNER DEVELOPER.

Problem: When I test my obby, the Gamepass gears work perfectly right? Wrong! As soon as I died and respawned on the checkpoint, my gears were gone?!? Let me know if you have an answer to this problem!

Edit: I have tried the answerer's script but all it did was fling me up in the sky and then loopkilling me?

Answerer's Script:

local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
 
local gamePassID = 0000000  -- Change this to your game pass ID
 
local function onCharacterAdded(player)
 
	local hasPass = false
 
	-- Check if the player already owns the game pass
	local success, message = pcall(function()
		hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)
	end)
 
	-- If there's an error, issue a warning and exit the function
	if not success then
		warn("Error while checking if player has pass: " .. tostring(message))
		return
	end
 
	if hasPass == true then
		game.ServerStorage.GearExample:Clone().Parent = player.Backpack
		-- Assign this player the ability or bonus related to the game pass
		--
	end
end
 
-- Connect "PlayerAdded" events to the "onPlayerAdded()" function
Players.CharacterAdded:Connect(onPlayerAdded)

3 comments

0

have you checked the output?

0

No

0

Sorry I don't understand I am new at Roblox Studio btw

1 answer

✓ Accepted answer

The PlayerAdded event only works when the player joins the game so when they die, the tool doesn't get re-cloned. The CharacterAdded event is fired every time the player respawns, so every time the player respawns, the gear gets put into the player's backpack again. New code:

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

local gamePassID = 0000000  -- Change this to your game pass ID

local function onCharacterAdded(character)
    local player = game.Players:GetPlayerFromCharacter(character)
    local hasPass = false

    -- Check if the player already owns the game pass
    local success, message = pcall(function()
        hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)
    end)

    -- If there's an error, issue a warning and exit the function
    if not success then
        warn("Error while checking if player has pass: " .. tostring(message))
        return
    end

    if hasPass == true then
        game.ServerStorage.GearExample:Clone().Parent = player.Backpack
        -- Assign this player the ability or bonus related to the game pass
        --
    end
end

-- Connect "PlayerAdded" events to the "onPlayerAdded()" function
Players.CharacterAdded:Connect(onPlayerAdded)

The chara

3 comments

0

OMG THANK YOU SO MUCH!

0

Wait

0

Cucu it just kept flinging up into the sky and loopkilling me?!?

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