My .Touched event doesn't fire if the player has previously died?

game.Workspace.DustyDunes.Badges.Desert1.PrimaryPart.Touched:Connect(function(pot)
    local hum = pot.Parent:FindFirstChild("Humanoid")
	Character.Humanoid.WalkSpeed = 0
	if hum == Character.Humanoid  and detect == 0 then
		detect = 1
		print("Badge Gained!")
		process("Floating On The Sand", 10, 20, game.Workspace.DustyDunes.Cameras.Desert1.CFrame)
	end
end)

This code works completely fine if the player has not died at all since opening the client (or in my case, during playtesting). Otherwise, it will not run at all. The humanoid’s walkspeed is not altered, ‘Badge Gained’ is not printed, and the process function is not executed. However, I do not get an error in the Output section.
Is this something to do with the Character being reset, or is it something else I’m overlooking? This is a local script located in StarterPlayerScripts.

Does the definition of the “Character” variable get updated when the character respawns after death?

2 Likes

Hello Dave,
From a first glance your issue is most likely that the Character var is not being updated after you respawn.
If we could see the entire script that’d be tremendously helpful, may I also ask what kind of script this is and it’s location as getting as much information as we can means we can help diagnose the issue more effectively and quickly.

You shouldn’t define Character before the touched event, rather something like this


local Part = game.Workspace.DustyDunes.Badges.Desert1.PrimaryPart

Part.Touched:Connect(function(pot)
  local Character
  if pot.Parent:FindFirstChild("Humanoid") then
    Character = Pot.Parent
    local hum = pot.Parent:FindFirstChild("Humanoid")
	Character.Humanoid.WalkSpeed = 0
	if hum == Character.Humanoid  and detect == 0 then
		detect = 1
		print("Badge Gained!")
		process("Floating On The Sand", 10, 20, game.Workspace.DustyDunes.Cameras.Desert1.CFrame)
	end
  end
end)

Sorry for indents.
1 Like

Like @realhigbead was saying and @RainScripts showed, make sure that your script can tell what the character is when it spawns, having it before the touched event doesn’t signify “Character” correctly.

Thank you very much for the code, it works consistently now.
The reason I was initially uncertain that the Character was the problem is because it isn’t directly involved with the firing of the event. I definitely should have seen that it was part of everything inside the event.

1 Like