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

hit.Parent sometimes valid but sometimes hit is nil. Why?

So, sometimes on line 6 (in this script and another nearly identical one) it says "attempted to index nil with Parent. After doing some test I believe that it means hit is returning nil. Why? Sometimes when I start the game it will work fine and hit will return as it normally would (Both in-studio and in-game), but sometimes it will return nil. In the local script the player is teleport to a part 0.5 seconds after they touch the pad, but changing this to more time and less time didn't seem to affect it.

local vamp = game.ReplicatedStorage.Vampire

game.ReplicatedStorage.VampPad.OnServerEvent:Connect(function(player, hit)
	print("GIVING VAMPIRE!")
	local cvamp = vamp:Clone()
	cvamp.Parent = hit.Parent
	cvamp.CFrame = hit.Parent.Torso.CFrame
	print("welding")
	local weld = Instance.new("Weld")
	weld.Part0 = hit.Parent.Torso
	weld.Part1 = cvamp
	weld.C0 = hit.Parent.Torso.CFrame:Inverse()
	weld.C1 = cvamp.CFrame:Inverse()
	weld.Parent = cvamp
	hit.Parent.Humanoid.WalkSpeed = 20
end)

Here's the Server script:

local sgiver = game.Workspace.GiveVampire
local plr = game.Players.LocalPlayer
local VampGiver = game.ReplicatedStorage.VampPad
local CD = false

sgiver.Touched:Connect(function(hit)
	if hit.Parent:WaitForChild("Humanoid") ~= nil and CD == false then
		CD = true
		print("Touched")
		-- gives vampire to player (code below)
		wait()
		VampGiver:FireServer(hit)
		print("Fired")
		-- Teleports Player
		wait(0.5)
		hit.Parent.Torso.CFrame = game.Workspace.SpawnPart.CFrame
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 20
		wait(0.5)
		-- This is the cooldown V
		CD = false
	end
end)

I don't know if this is relevant, but the game is a team create game. That's also why there are so many notes.

3 comments

0

Since "hit" is one of the arguments passed to the function which is connected to the Remote, it's very difficult for us to spot why "hit" is nil. Could you provide us with the script that is firing the Remote?

0

Yes, I'll add that right now

0

Ok I added the server script

1 answer

There is slight chance that character that touched the part has de-spawned (died) before server had the chance to process the event. Limb that touched the giver has been already parented to nil (destroyed), hence the error.

There is also a serious issue in the LocalScript (bottom one, even though it is described as server script). You are using WaitForChild instead of FindFirstChild. This way you fill call stack very fast with lots of unnecessary, infinite yielding calls, every time giver is touched by non-humanoid.

Finally, teleport HumanoidRootPart and make your game both RTHRO and R15 compatibile.

First issue solution:

if not hit or not hit.Parent then
	print("Character not found!")
	return --this will exit the function
end
print("GIVING VAMPIRE!")
local cvamp = vamp:Clone()
cvamp.Parent = hit.Parent

FindFirstChild usage:

if hit.Parent:FindFirstChild("Humanoid") ~= nil and CD == false then

HumanoidRootPart teleport:

hit.Parent.HumanoidRootPart.CFrame = game.Workspace.SpawnPart.CFrame

I hope this helps!

1 comment

0

Oh im sorry! I forgot to edit the script. It turns out the custom hitbox I have on the players was interfering with it, so I ended up just checking if hit.Name wasnt the hitbox name and it works fine. Sorry!

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