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

This doesn't make any sense, why can I only call this function in certain areas?

Asked by 8 years ago
Edited 8 years ago

Hello everyone, I made a function that awards a currency called skillpoints when ever it is called. The input for the function requires a player and the amount of skill points you want to reward. However even though I defined the function, it throws a nil value. I pretty much setup where I wanted to add skill points as the same where I already added them and they work.

Here is the nil error:

23:05:40.336 - ServerScriptService.MainScript:573: attempt to call global 'awardskillpoints' (a nil value)
23:05:40.337 - Stack Begin
23:05:40.338 - Script 'ServerScriptService.MainScript', Line 573
23:05:40.338 - Stack End

For some reason it is saying my function, known as "awardskillpoints" is non existent. Here is where I want my function to work.

local localtimer = roundtimer
while localtimer > 0 do
    wait(1)
    localtimer = localtimer - 1
    timertag.Value = localtimer
    aliveplayers = {}
    killeralive = false
    officeralive = false
    subofficeralive = false
    civilianalive = false
    for _, player in pairs(players) do
        if player ~= nil then
            local character = player.Character
            if character ~= nil then
                local alivetag = character:FindFirstChild("AliveTag")
                local humanoid = character:FindFirstChild("Humanoid")
                if alivetag ~= nil and humanoid ~= nil and humanoid.Health > 0 then
                    if player == killer then
                        killeralive = true
                    elseif player == officer then
                    officeralive = true
                    elseif player == subofficer then
                        subofficeralive = true
                    elseif player == civilian then
                        civilianalive = true
                    end
                    table.insert(aliveplayers, player)
                end
            end
        end
    end
    for _, player in pairs(players) do
        if player ~= nil and player == civilian and civilianalive == true and localtimer == 290 then
            awardskillpoints(player, 1) -- This is being nil
        event:FireClient(player, "GotPoints", "1")
    end
end
    -- if one player is alive, or killer is dead.
    if #aliveplayers <= 1 or killeralive == false then
        break
    end
end

Here is my function:

function awardskillpoints(player, skillpoints)
    if player ~= nil and skillpoints ~= nil and skillpointdebounce == false then
        skillpointdebounce = true
        local mydata = playerdata[player.userId]
        if mydata ~= nil then
            mydata.skillpoints = mydata.skillpoints + skillpoints
            local skillpointsvalue = player:FindFirstChild("Skill Points")
            if skillpointsvalue ~= nil then
                skillpointsvalue.Value = mydata.skillpoints
                delay(0.00001, function ()
                    skillpointdebounce = false
            end)
        end
    end
    end
end
end

Finally, here are the parts that work calling this function.

    for _, player in pairs(players) do
            if matchresults == "KillerWins" and player == killer then
                awardskillpoints(player, 4)
                awardwin(player)
                awardxp(player, 100)
            elseif matchresults == "CiviliansWin" and player ~= killer and player ~= officer and player ~= subofficer then
                awardskillpoints(player, 5)
                awardwin(player)
                awardxp(player, 100)
            elseif matchresults == "OfficerWins" and player == officer then
                awardskillpoints(player, 5)
                awardwin(player)
                awardxp(player, 100)
            elseif matchresults == "SuvOfficerWins" and player == subofficer then
                awardskillpoints(player, 6)
                awardwin(player)
                awardxp(player, 100)
            elseif player == killer or player == officer or player == civilian or player == subofficer then
                awardxp(player, 50)
            else
                awardskillpoints(player, 0)
            end
        end
end

In this section of script, the function works perfectly no issues at all.

Here is another area where I used it.

for _, player in pairs(aliveplayers)do
    delay(1.2, function()
        awardskillpoints(player, 1)
    end)

just a section, but this worked fine too.

Any ideas will help, Not that I expect this question to be answered since more than 3/4 of my questions seemed to have been ignored.

Thank you in advanced.

0
It could be that you are trying to call the function before it's been defined. IDidMakeThat 1135 — 8y
0
forgot to mention but, the function is defined at the very top. Dekadrachm 50 — 8y
0
just an idea, throw a brief wait before the while loop that is giving you trouble. I mean the error means that for whatever reason the function doesn't exist when it's called.. might be a timing issue (even though you declare it at the top) patrline5599 150 — 8y
0
another possibility is that the error is for the wrong function entirely (idk why that would be), but does the event:FireClient work? patrline5599 150 — 8y
0
The event client does work, I did find a method to do it, which was defining everything a "weird" way. Dekadrachm 50 — 8y

1 answer

Log in to vote
0
Answered by 8 years ago

You could try to do what i always do. Place all your functions before the script so before putting the entire rest of your script just slap all your functions there.

Ad

Answer this question