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

Touch detector not detecting hit?

I have this code:

script.Parent.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("HumanoidRootPart")
	if humanoid then
		if totalWins < 1 then
			totalWins = 1
		elseif totalWins == 1 then
			totalWins = 0
			destroyStages()
			wait(3)
			generateStages()
		end
		hit.Parent:LoadCharacter()
		hit.Parent.leaderstats.Wins = hit.Parent.leaderstats.Wins + 1
	end
end)

Everything in the code is defined but when the player touches the block, none of the stuff happens.

3 comments

0

add print()s to find out where the code stops then come back

0

code stops after line 2, error message was that I compared instance and value so I'm pretty sure the issue is that line 4 should say totalWins.Value

0

I updated it with totalWins.Value and it still stops after line 2.

2 answers

✓ Accepted answer

Your code can be fixed using the GetPlayerFromCharacter function. In which will acquire the character's player. Here is your fixed script:

script.Parent.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- This is it
    local humanoid = hit.Parent:FindFirstChild("HumanoidRootPart")
    if humanoid then
        if totalWins < 1 then
            totalWins = 1
        elseif totalWins == 1 then
            totalWins = 0
            destroyStages()
            wait(3)
            generateStages()
        end
        player:LoadCharacter()
        player.leaderstats.Wins += 1 -- Compound Operator '+='
    end
end)

I hope this helped, if you encounter any problems, just comment. If you found this helpful, then consider accepting this answer, and you can also upvote if you want, I gladly appreciate.

You didnt set totalWins to anything. I guess that its a value from a leaderstats, so you can just do:

local totalWins = game.Players:FindFirstChild(hit.Parent.Name).leaderstats.totalWins

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