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

Award Leaderstats not working? Please help.. [Answered]

Asked by 10 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.

I have 2 scripts inside 1 part... the first script insert a Int. Value and that script works fine... I want this to be a script that when you touch it it changes the leaderstats Value Please help

function onTouched(hit)
    local player = hit.Parent
    Logs = player.leaderstats.Logs
    Logs.Value = Logs.Value + 5
end
script.Parent.Touched:connect(onTouched)
0
Check mine. It should work. NinjoOnline 1146 — 10y
0
How is this -1 point? Timster111 25 — 10y

3 answers

Log in to vote
-1
Answered by 10 years ago

Add this to workspace as the leaderboard.

game.Players.PlayerAdded:connect(function(player)
    local stats = Instance.new("IntValue", player)
    stats.Name = "leaderstats"

    local logs = Instance.new("IntValue", stats)
    logs.Name = "Logs"
    logs.Value = 0
end)

Put inside part

game.Players.PlayerAdded:connect(function(player)
    script.Parent.Touched:connect(function(hit)
        local logs = player.leaderstats.Logs
        logs.Value = logs.Value + 5
        wait(2) -- To stop constant spamming
    end)
end)

This should work as it has worked for me. Hope I helped and please leave a rate up and accept answer for everyone else.

- NinjoOnline

Ad
Log in to vote
0
Answered by
Kratos232 105
10 years ago

The problem with your script is that A) You're assuming that ANYTHING that touches that part, is part of a Player. It could just be a random part that falls on it. :P and B) The line local player = hit.Parent

What's wrong with that is that, say I go and touch the part, Okay? It'll just say that my Leg/Arm's Parent is a Player, when it's ACTUALLY my character. (Unless you keep your leaderstats in people's character, then all of this is wrong. :c )

So, first you need to make sure it's a Player, and then get the Player. You can do both of these, by just using the :GetPlayerFromCharacter() method, in Players, like so.

Players = game:GetService("Players") --// To get the Players. Just 'cos.

function onTouched(hit)
    local player = Players:GetPlayerFromCharacter(hit.Parent)
    if Player ~= nil then
        --// Code
    end
end

script.Parent.Touched:connect(onTouched)

You see, the :GetPlayerFromCharacter() method does, basically, what it's name implies it does. With this, it will get the Player's Player, and not the Character.

Now that you have the Player, all that's left is to award the points, and, assuming it'd called Logs like in your script example, I'll just try add it like this...

Players = game:GetService("Players") --// Gets Players

function onTouched(hit)
    local player = Players:GetPlayerFromCharacter(hit.Parent) --// Gets a Players Player from their Character
    if Player ~= nil then --// Checks if it's real.
        if player and player.leaderstats and player.leaderstats.Logs then --// Checks if the leaderstats exist (I don't even know if this works, honestly.
            player.leaderstats.Logs.Value = player.leaderstats.Logs.Value + 5 --// Awards Points
        end
    end
end

script.Parent.Touched:connect(onTouched)

Well, it should work. Looks about right...

Hope this helped. :) If it didn't please don't hate me. :(

  • Kratos232
Log in to vote
-2
Answered by
Relatch 550 Moderation Voter
10 years ago
function onTouch(hit)
    local logs = hit.Parent.leaderstats.Logs
    logs.Value = logs.Value + 5
end
script.Parent.Touched:connect(onTouch)

Try and add this to workspace.

game.Players.PlayerAdded:connect(function(plr)
    leaderstats = Instance.new("IntValue")
    leaderstats.Name = "leaderstats"

    logs = Instance.new("IntValue")
    logs.Name = "Logs"

    logs.Parent = leaderstats
    leaderstats.Parent = plr
end)
0
Still isnt working... Timster111 25 — 10y
0
It says in the output "leaderstats is not a valid member of Model" and the leaderstats are inside the Service "Player" Timster111 25 — 10y
0
why thumbs down qq Relatch 550 — 10y
0
How would I fix this? Timster111 25 — 10y
View all comments (3 more)
0
Not sure. Relatch 550 — 10y
0
:/ Timster111 25 — 10y
0
Updated. Relatch 550 — 10y

Answer this question