(HELP) Gravity Coil gamepass stops working after a player resets/dies

Hello! I tried to follow a scripting tutorial on how to create a gravity coil, and it seems that the game can’t find Handle for the tool in order to put it in the Player’s backpack after they reset. The coil works fine, until the player dies. Here is the script:

task.wait(2)

local Players = game:GetService("Players")

local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local tool = script.Parent
local handle = tool:WaitForChild("Handle")
local coilSound = handle:WaitForChild("CoilSound")

local JUMP_HEIGHT_PERCENTAGE = math.clamp(tool:GetAttribute("JumpHeightPercentage"), 0, 1)

local toolEquipped = false
local gravityEffectAttachment
local gravityEffect

local function IsCharacterAlive()
	local rootPart = character:FindFirstChild("HumanoidRootPart")
	local humanoid = character:FindFirstChild("Humanoid")

	return (rootPart and humanoid and humanoid.Health > 0)
end

local function GetAllConnectedParts(object)
	local parts = {}

	local function GetConnectedParts(object)
		for i, v in pairs(object:GetConnectedParts()) do
			local ignore = false

			for ii, vv in pairs(parts) do
				if v == vv then
					ignore = true
				end
			end

			if not ignore then
				parts[#parts + 1] = v
				GetConnectedParts(v)
			end
		end
	end

	GetConnectedParts(object)
	return parts
end

local function SetGravityEffect()
	if not toolEquipped or not IsCharacterAlive() then
		return
	end

	local rootPart = character.HumanoidRootPart
	local connectedParts = GetAllConnectedParts(rootPart)
	local totalMass = 0

	for i, v in pairs(connectedParts) do
		if v:IsA("BasePart") then
			totalMass += v:GetMass()
		end
	end

	gravityEffect.Force = Vector3.new(0, totalMass * workspace.Gravity * (1 - JUMP_HEIGHT_PERCENTAGE), 0)
	gravityEffect.Enabled = true
end

local function RemoveGravityEffect()
	if gravityEffect and gravityEffect.Parent then
		gravityEffect.Enabled = false
	end
end

tool.Equipped:Connect(function()
	toolEquipped = true
	coilSound:Play()
	SetGravityEffect()
end)

tool.Unequipped:Connect(function()
	toolEquipped = false
	RemoveGravityEffect()
end)

gravityEffectAttachment = Instance.new("Attachment")
gravityEffectAttachment.Name = "GravityEffectAttachment"
gravityEffectAttachment.Parent = character:WaitForChild("HumanoidRootPart")

gravityEffect = Instance.new("VectorForce")
gravityEffect.Name = "GravityEffect"
gravityEffect.Attachment0 = gravityEffectAttachment
gravityEffect.Force = Vector3.new(0, 0, 0)
gravityEffect.Enabled = false
gravityEffect.Parent = character.HumanoidRootPart

The task.wait() I tried doesn’t seem to relieve the infinite yield error. Thanks!

1 Like

Workin on it will tell u when done

2 Likes

Where is the tool located at ?

Yes. When a player purchases the gamepass, it receives it automatically.

well i need that to detect when the player dies and respawns then it checks if u own the gamepass and gives the coil to u
also when the player jojns

Under a script in workspace. The localscript listed up there is located as a child under the coil.

local MarketplaceService = game:GetService(“MarketplaceService”)
local Players = game:GetService(“Players”)

local tool = script:WaitForChild(“GravityCoil”)
local gamePassId = script:WaitForChild(“PassID”).Value

local owners = {}

MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(player, passId, purchased)
if purchased and passId == gamePassId then
local coil = tool:Clone()
coil.Parent = player.Backpack
owners[“ID_” … player.UserId] = true
end
end)

Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function()
wait()

	local ownsPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassId)

	if ownsPass or owners["ID_" .. player.UserId] then
		local coil = tool:Clone()
		coil.Parent = player.Backpack
	end
end)

end)

actually u need to do ``` for the code to do its thing :nerd:

local MarketplaceService = game:GetService(“MarketplaceService”)
local Players = game:GetService(“Players”)

local tool = script:WaitForChild(“GravityCoil”)
local gamePassId = script:WaitForChild(“PassID”).Value

local owners = {}

MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(player, passId, purchased)
if purchased and passId == gamePassId then
local coil = tool:Clone()
coil.Parent = player.Backpack
owners[“ID_” … player.UserId] = true
end
end)

Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function()
wait()

	local ownsPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassId)

	if ownsPass or owners["ID_" .. player.UserId] then
		local coil = tool:Clone()
		coil.Parent = player.Backpack
	end
end)
end)

however i do think it works*

I found the solution, where I was supposed to run the script in ServerScriptService instead of Workspace. Thanks!