Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

OnTouched Part giving +1 score to Player Touched

Asked by
RSoMKC 45
6 years ago

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

Log in to vote
1
Answered by
nate890 490 Moderation Voter
6 years ago
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)
Ad
Log in to vote
3
Answered by 6 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:

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)
Log in to vote
0
Answered by 6 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