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

OnTouched Part giving +1 score to Player Touched

I have this block of code, however my onTouched doesn't function.

local Finish = MapWin.Finish
	Finish.onTouched:connect(function(player)
	player.leaderstats.Score.Value = player.leaderstats.Score.Value +1
	Finish:Destroy()

As you can see I want the player that first touched Finish to get +1 score. Afterwards I want the Finish destroyed. (In order to keep the player from scoring repeatedly)

The error log I receive is: - leaderstats is not a valid member of Model

3 answers

✓ Accepted answer
local Finish = MapWin.Finish

Finish.Touched:connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		player.leaderstats.Score.Value = player.leaderstats.Score.Value +1
		Finish:Destroy()
	end
end)

There are multiple problems in your code.

Firstly, you have a syntax error. You need to use the end keyword fallowed by a paranthesis to end your function statement.

Secondly, the event is Touched, not onTouched.

Finally, the Touched event returns the other part that was touched, not the player. To get the player from the character that touched it, you can use the following code:

any_part.Touched:connect(function(part)
	if part.Parent and game.Players:playerFromCharacter(part.Parent) then
		local player = game.Players:playerFromCharacter(part.Parent)
		
		player.leaderstats.Score.Value = player.leaderstats.Score.Value + 1 -- etc
	end
end)

Would this work? local ting = 0 function onTouched(hit)

if ting == 0 then ting = 1 check = hit.Parent:FindFirstChild("Humanoid")

if check ~= nil then

local user = game.Players:GetPlayerFromCharacter(hit.Parent) local stats = user:findFirstChild("leaderstats")

if stats ~= nil then -- local cash = stats:findFirstChild("Points") --change points to name of leaderboard stat cash.Value = cash.Value +1 wait() end

end

ting = 0 end

end

script.Parent.Touched:connect(onTouched)

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