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

Any ideas why my script won't change the leaderboard value?

Asked by 9 years ago

Hello! I have a script that is supposed to give you money each second you drive, but it isn't exactly working.

script.Parent.Touched:connect(touch)

function touch(hit)
local character = hit.Parent
local stats = hit.Parent:findFirstChild("leaderstats")
local player = game.Players:GetPlayerFromCharacter(character)
local add = 110
wait(1)
if character.HumanoidRootPart and script.Parent.SeatWeld then
    print("Player sitting")
       if script.Parent.Throttle == 1 then
            wait(1)
            local cash = stats:findFirstChild("Money")
            cash.Value  = cash.Value +add
        end 
end
end
script.Parent.Touched:connect(touch)

It prints the player sitting message, but then comes up (and doesn't refer to any line in any script) saying attempt to call a nil value. Any ideas why it won't work? I have a leaderboard with a money section and everything.

0
Try putting a space inbetween add and the + drew1017 330 — 9y
0
@drew1017 1) If you're giving an answer, post an answer, not a comment. 2) Only under very rare circumstances does Lua care about white-space. While the code here could be spaced a lot better, that's only for the HUMANS dealing with it. BlueTaslem 18071 — 9y

2 answers

Log in to vote
1
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
9 years ago

Problem

Your problem with the script is the fact that the script can not find leaderstats in the character (Line 5). The leaderstats container is located in the player, which you do not find until line 6.


Recommendations

One thing that I do recommend you do for your script is, to detect if the hit part has a humanoid. As well as detect if the player variable had not returned nil if say, a model containing humanoid or a AI comes along and hits the brick.

Also, some of your spacing in your code is off. This progressive makes your code harder to read, by yourself and others. This may eventually lead you to forget or misplace ends for functions, loops, or if then statements.


Solution

local add = 110 --Amount of cash added when a player sits in the vehicle seat.

function touch(hit)
    local character = hit.Parent
    local player = nil
    if character:FindFirstChild('Humanoid') then --So we will check if Humanoid is in the model hitting the part.
        player = game.Players:GetPlayerFromCharacter(character) --Now we are trying to get the player value.
        if player then
            if player:FindFirstChild('leaderstats') then --Now we're detecting if the leaderstats is in the player or not.
                --With a new property released in May you are able to use a property called SeatPart in Humanoid to detect the seat you're sitting in.
                while wait(1) and character and player do --It will wait 1 second, check if there is a character and player, if both the player and character are there, then the code will go.
                    if character.Humanoid.SeatPart == script.Parent then
                        if player.leaderstats:FindFirstChild('Money') then --Chk 4 meh m0ney.
                            player.leaderstats.Money.Value = player.leaderstats.Money.Value + add
                        end
                    else --I know what you might be thinking. What if the SeatPart is nil? Well, we don't want the loop to go on forever, so we break it.
                        break
                    end
                end             
            end
        end
    end
end

script.Parent.Touched:connect(touch) --We're connecting the function touch above this line.

Remember, if this answer helped, do not forget to upvote. If I solved your problem do not forget to hit that Accept Answer button to the right.
0
This works, but it only gives the money once, then you have to get up and sit down again for it to give you more money. How do I fix this? I'd like it to give the money every second. blueburns 10 — 9y
Ad
Log in to vote
-1
Answered by
Vid_eo 126
9 years ago

(I'm a beginner scripter, so sorry if this doesn't work)

But the problem is that the nil value is add. The 14th line should be

cash.Value = cash.Value + 1

Hope this helps! Instead of add, put 1.

Answer this question