Medkit system doesn't work PLEASE HELP

I am Jimmy and I am trying to make a cool medkit system for my game with 6mil visits called SUSSY IMPOSTER LAND, where a medkit will drop with a tween a few studs below a player’s humanoidrootpart when they die, which can only be used by a different player (not the player who died). When it’s used it is supposed to tween up and be destroyed.

The issue: This works perfectly fine in studio BUT IN GAME IT BREAKS AND NOBODY CAN PICK THEM UP… and they aren’t removing so they end up causing mass clutter. I don’t see an error when I join the game.

I’ve tried checking what might cause the error and couldn’t tell. Any help is appreciated bro. THX!

Sincerely, Jimmy

local medkit = script.Medkit
local Players = game:GetService("Players")

local tweenService = game:GetService("TweenService")
local replicatedStorage = game:GetService("ReplicatedStorage")

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			local humanoidRootPart = player.Character:FindFirstChild("HumanoidRootPart")

			if humanoidRootPart then
				local kitclone = medkit:Clone()
				kitclone.Anchored = true
				kitclone.CanCollide = false		
				kitclone.Position = humanoidRootPart.Position
				kitclone.Parent = game.Workspace

				local sound = replicatedStorage.Sounds.Heal:Clone()
				sound.Parent = kitclone

				local t = tweenService:Create(kitclone, TweenInfo.new(0.8, Enum.EasingStyle.Bounce, Enum.EasingDirection.Out), {Position = Vector3.new(kitclone.Position.X, kitclone.Position.Y - 1.15, kitclone.Position.Z)})
				t:Play()
				local tweenInfo = TweenInfo.new(1,Enum.EasingStyle.Sine,Enum.EasingDirection.In)
				
				task.spawn(function()
					local function TweenCreate()
						local Props = {Orientation = Vector3.new(0, kitclone.Orientation.Y + 360,0)}
						local tweenRotate = tweenService:Create(kitclone, tweenInfo, Props)
						tweenRotate:Play()
						return tweenRotate
					end
					while kitclone do 
						task.wait()
						local tween = TweenCreate()
						tween.Completed:Wait() 
						tween:Destroy() 
					end
				end)

				local touchedConnection = kitclone.Touched:Connect(function(t)
					local humanoid = t.Parent:FindFirstChild("Humanoid")
					if humanoid and t.Parent.Name ~= player.Name then
						removeKit()
						local amount = humanoid.Health + humanoid.MaxHealth * 0.7
						if amount > humanoid.MaxHealth then 
							amount = humanoid.MaxHealth
						end
						t.Parent.Humanoid.Health = amount
						sound:Play()
						return
					else
						print(player.Name .. " is creator healthkit" .. t.Parent.Name .. " is ur mom who took it and it didn't work bc idk")
					end
				end)

				function removeKit()

					print("Removing Kit.")
					touchedConnection:Disconnect()
					
					local tT = tweenService:Create(kitclone, TweenInfo.new(0.35, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {Transparency = 1})
					tT:Play() 

					task.wait(0.35)
					kitclone:Destroy()
				end

				task.wait(50)

				removeKit()
			end
		end)
	end)
end)
3 Likes

any errors in the developer console?

I may be wrong, but I’m pretty sure you need to define removeKit() above the touchedConnection. You would also need to put this above where you define removeKit():

local touchedConnection = nil

basically it would look like this:

local medkit = script.Medkit
local Players = game:GetService("Players")

local tweenService = game:GetService("TweenService")
local replicatedStorage = game:GetService("ReplicatedStorage")

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			local humanoidRootPart = player.Character:FindFirstChild("HumanoidRootPart")

			if humanoidRootPart then
				local kitclone = medkit:Clone()
				kitclone.Anchored = true
				kitclone.CanCollide = false		
				kitclone.Position = humanoidRootPart.Position
				kitclone.Parent = game.Workspace

				local sound = replicatedStorage.Sounds.Heal:Clone()
				sound.Parent = kitclone

				local t = tweenService:Create(kitclone, TweenInfo.new(0.8, Enum.EasingStyle.Bounce, Enum.EasingDirection.Out), {Position = Vector3.new(kitclone.Position.X, kitclone.Position.Y - 1.15, kitclone.Position.Z)})
				t:Play()
				local tweenInfo = TweenInfo.new(1,Enum.EasingStyle.Sine,Enum.EasingDirection.In)

				task.spawn(function()
					local function TweenCreate()
						local Props = {Orientation = Vector3.new(0, kitclone.Orientation.Y + 360,0)}
						local tweenRotate = tweenService:Create(kitclone, tweenInfo, Props)
						tweenRotate:Play()
						return tweenRotate
					end
					while kitclone do 
						task.wait()
						local tween = TweenCreate()
						tween.Completed:Wait() 
						tween:Destroy() 
					end
				end)
				
				local touchedConnection = nil
				
				local function removeKit()

					print("Removing Kit.")
					touchedConnection:Disconnect()

					local tT = tweenService:Create(kitclone, TweenInfo.new(0.35, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {Transparency = 1})
					tT:Play() 

					task.wait(0.35)
					kitclone:Destroy()
				end

				touchedConnection = kitclone.Touched:Connect(function(t)
					local humanoid = t.Parent:FindFirstChild("Humanoid")
					if humanoid and t.Parent.Name ~= player.Name then
						removeKit()
						local amount = humanoid.Health + humanoid.MaxHealth * 0.7
						if amount > humanoid.MaxHealth then 
							amount = humanoid.MaxHealth
						end
						t.Parent.Humanoid.Health = amount
						sound:Play()
						return
					else
						print(player.Name .. " is creator healthkit" .. t.Parent.Name .. " is ur mom who took it and it didn't work bc idk")
					end
				end)

				

				task.wait(50)

				removeKit()
			end
		end)
	end)
end)

Thanks so much I gtg but I’ll test it tomorrow and let you know