I have this block of code, however my onTouched doesn't function.
1 | local Finish = MapWin.Finish |
2 | Finish.onTouched:connect( function (player) |
3 | player.leaderstats.Score.Value = player.leaderstats.Score.Value + 1 |
4 | 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
1 | local Finish = MapWin.Finish |
2 |
3 | Finish.Touched:connect( function (hit) |
4 | local player = game.Players:GetPlayerFromCharacter(hit.Parent) |
5 | if player then |
6 | player.leaderstats.Score.Value = player.leaderstats.Score.Value + 1 |
7 | Finish:Destroy() |
8 | end |
9 | 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:
1 | any_part.Touched:connect( function (part) |
2 | if part.Parent and game.Players:playerFromCharacter(part.Parent) then |
3 | local player = game.Players:playerFromCharacter(part.Parent) |
4 |
5 | player.leaderstats.Score.Value = player.leaderstats.Score.Value + 1 -- etc |
6 | end |
7 | 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)