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

Why doesn't this tool with math.random work at all?

Asked by 6 years ago

I have a tool that is supposed to give the player stats when activated, however there are no errors and it doesn't seem to be working. The problem I see is at

script.Parent.Activated:Connect(function()

because nothing is printed after that line of code. Any help would be appreciated!

By the way, I've tried this with both filtering enabled on and off, and with a local script and a normal script. All it contains is just a tool in starterpack with a script as the tool's child.

local player = script.Parent.Parent.Parent
print("line 2")
db = false
print("line 4")
script.Parent.Activated:Connect(function()
    if db == false then
        print("line 6")
        db = true
        local randomm = math.random(1,2)
        print(1)
        if randomm == 1 then
            print("1")
            player.Stats.Strength.Value = player.Stats.Strength.Value + 1   
        end

        if randomm == 2 then
            print("2")
            player.Stats.Strength.Value = player.Stats.Strength.Value + 2   
        end
    end
    wait(1)
    print("db = false")
    db = false
end)
0
Not exactly related to the answer you want but something to clear up thats irking me a bit. Why not make it so that the value added (1 or 2) is the "randomm" variable instead, you can easily remove 6 lines of code from your current script by doing that. I did think that maybe you have something in mind with those "randomm" if statements but im just making sure with you, that is all Zukaazora 30 — 6y
0
Once I get it working I'm going to add more random statements with animations for each one. Draebrewop 114 — 6y
0
It's bothering me how "randomm" is spelled wrong. hiimgoodpack 2009 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago

I don't see any problems, if it's in a tool, make sure the tool either has a child named "Handle" or change it so that it doesn't require a handle. Your script looks fine.

0
Thanks! I guess I forgot to change the requires handle property... Draebrewop 114 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

If the player has to equip the tool to use it, then it is located within their character at the time of activation. If their stats are located in the player, then a tool in the character would not be able to access the stats through a string of parents.

If you change

local player = script.Parent.Parent.Parent

to

local player = game.Players:GetPlayerFromCharacter(script.Parent.Parent)

then the rest of the script should work as it is.

0
That unfortunately didn't fix it, the error is "Attempt to index local 'player'" Draebrewop 114 — 6y
0
Where is the script located? hiimgoodpack 2009 — 6y

Answer this question