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

I want my Wepon (a Stick) to do RANDOM Damage but its not..Why!?

here is my script for my Stick Wepon

on line 5, isn't it a table of numbers?

on line 18 I thought it would pick a random damage nuber between in the values but its not...

What is wrong

local fired = script.Parent.Remotes.damage	
local candmg = script.Parent.CanDamage


local damage = {7,5,6}



-------
fired.OnServerEvent:Connect(function(player)
	candmg = true
end)
script.Parent.Handle.Touched:Connect(function(hit)
	if candmg == true then
		candmg = false
		local ehum = hit and hit.Parent:FindFirstChild("Humanoid")
		if ehum then
			ehum:TakeDamage(damage[math.random()])
			print("You Dealed " .. damage.." Damage")
			
		else
			print("the Humanoid you ARE looking for is NIL")
			candmg = false
		end
	end
end)


--Scripted by Tizzel40

now it look like this but now it wont print that random damage number!

on line 19

local fired = script.Parent.Remotes.damage	
local candmg = script.Parent.CanDamage


local damage = math.random(5,7,8)



-------
fired.OnServerEvent:Connect(function(player)
	candmg = true
end)
script.Parent.Handle.Touched:Connect(function(hit)
	if candmg == true then
		candmg = false
		local ehum = hit and hit.Parent:FindFirstChild("Humanoid")
		if ehum then
			ehum:TakeDamage(damage)
			print("You Dealed " .. damage .." Damage")
			
		else
			print("the Humanoid you ARE looking for is NIL")
			candmg = false
		end
	end
end)


--Scripted by Tizzel40

2 comments

0

local damage = math.random(5,7)

0

omlllllllll thats cause ur setting it to that certain number, put damage after line 14

2 answers

I would recommend using a math.random()

Feks.

damage = math.random(7)

--later... 

ehum:TakeDamage(damage)

Something like that.

4 comments

0

but no its not printing that random damage number..

0

Any errors?

0

i fixed it myself

0

Great! :)

You're doing the table wrong and using the math.random() incorrectly.

A table is identified by the computer as "{ }" instead of "( )". The math.random(minimum, maximum) can only contain two parameters. The minimum and maximum values restrict the function from choosing a random value from outside the boundaries.

Here's a way of doing what you want:

local damage = {5, 6, 7}  --this is a table

print(damage[math.random(1, 3)]) --chooses one of the following: damage[1] = 5; damage[2] = 6; damage[3] = 7

In the output, the above script will print a random value from 'damage' table.

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