Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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)

3 comments

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

0

Once I get it working I'm going to add more random statements with animations for each one.

0

It's bothering me how "randomm" is spelled wrong.

2 answers

✓ Accepted answer

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.

1 comment

0

Thanks! I guess I forgot to change the requires handle property...

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.

2 comments

0

That unfortunately didn't fix it, the error is "Attempt to index local 'player'"

0

Where is the script located?

This discussion is preserved as it was. New answers, comments, and votes are closed.