Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

OnTouched Part giving +1 score to Player Touched

Asked by
RSoMKC 45
11 years ago

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

1local 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

3 answers

Log in to vote
1
Answered by
nate890 495 Moderation Voter
11 years ago
1local Finish = MapWin.Finish
2 
3Finish.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
9end)
Ad
Log in to vote
3
Answered by 11 years ago

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:

1any_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
7end)
Log in to vote
0
Answered by 11 years ago

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)

Answer this question