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

Why is the player nil in this touch script?

local function OnTouched(hit)
	if debounce and enabled and hit then
		debounce = false						-- no touchy again for a moment
		
		local hint = Instance.new("Hint", workspace)
		hint.Text = "Locating Plexus..."		
		
		if teleportTarget then
			
			local startPoint = teleportTarget:FindFirstChild("StartPoint")
			if startPoint then
				local startPointValue = startPoint.Value
				
				local player = game.Players:GetPlayerFromCharacter(hit.Parent)

				if player and player.Character and player.Character:FindFirstChild("UpperTorso") then

					-- Add player to the team, for tracking
					player.Team = Teams:FindFirstChild("Easy")
					player.Neutral = false

					-- Teleport player to the start point		
					player:SetPrimaryPartCFrame(CFrame.new(startPointValue))
				end

				hint.Text = "Entered the " .. teamName .. " Plexus!  Find the goal and Good luck!"

			end
		else
			teleportTarget = nil			-- Reset the variable to try and locate again
			hint.Text = "Plexus not found..."
		end
				
		wait(3)
		hint:Destroy()

		debounce = true
		
	end	
end

parent.Touched:Connect(OnTouched)

For some reason, in the IF statement to verify the player and player parts, player is nil. I cannot figure out why.

2 comments

0

Hints are deprecated. Use actual GUI instead

0

try putting on line "30" teleportTarget = 0 or nil

1 answer

✓ Accepted answer

You first need to detect if the hit.Parent was the same character model, you can use game.Players:FindFirstChild(hit.Parent.Name) while detecting the character in case you do not find hit.Parent you can try hit.Parent.Parent however I have just sorted out your script to locate the 2 types.

Fixed code:

local function OnTouched(hit)
    if debounce and enabled and hit.Parent:FindFirstChild("Humanoid") then
        debounce = false   
                     -- no touchy again for a moment
        local hint = Instance.new("Hint", workspace)
        hint.Text = "Locating Plexus..."   
     
        if teleportTarget then
            local startPoint = teleportTarget:FindFirstChild("StartPoint")
            if startPoint then
                local startPointValue = startPoint.Value
                local player = game.Players:FindFirstChild(hit.Parent.Name)
                if player and player.Character and player.Character:FindFirstChild("UpperTorso") then

                    -- Add player to the team, for tracking
                    player.Team = Teams:FindFirstChild("Easy")
                    player.Neutral = false

                    -- Teleport player to the start point       
                    player.Character:SetPrimaryPartCFrame(CFrame.new(startPointValue))
                end
                hint.Text = "Entered the " .. teamName .. " Plexus!  Find the goal and Good luck!"

            end
        else
            teleportTarget = nil            -- Reset the variable to try and locate again
            hint.Text = "Plexus not found..."
        end
        wait(3)
        hint:Destroy()
        debounce = true
	elseif debounce == true and enabled == true and hit.Parent.Parent:FindFirstChild("Humanoid") then
		debounce = false   
                     -- no touchy again for a moment
        local hint = Instance.new("Hint", workspace)
        hint.Text = "Locating Plexus..."   
     
        if teleportTarget then
            local startPoint = teleportTarget:FindFirstChild("StartPoint")
            if startPoint then
                local startPointValue = startPoint.Value
                local player = game.Players:FindFirstChild(hit.Parent.Parent.Name)
                if player and player.Character and player.Character:FindFirstChild("UpperTorso") then

                    -- Add player to the team, for tracking
                    player.Team = Teams:FindFirstChild("Easy")
                    player.Neutral = false

                    -- Teleport player to the start point       
                    player.Character:SetPrimaryPartCFrame(CFrame.new(startPointValue))
                end
                hint.Text = "Entered the " .. teamName .. " Plexus!  Find the goal and Good luck!"

            end
        else
            teleportTarget = nil            -- Reset the variable to try and locate again
            hint.Text = "Plexus not found..."
        end
        wait(3)
        hint:Destroy()
        debounce = true
    end 
end

parent.Touched:Connect(OnTouched)

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