Paramater understanding help

Im trying to understand how parameters work? Do they work like you send a value through them and then when you call it you can get the value?


local function updatedamg(idk) -- value want to be sent
	local Character = plr.Character or plr.CharacterAdded:Wait()
	local Axe = SearchForAxe(Character)
	local idk =	Axe:FindFirstChild("damage").Value
	print(idk.Value)
end

AddWoodEvent.OnServerEvent:Connect(function(Player) -- (A) Were you supposed to add "Wood" as a parameter
	local DamageValue = 0
	updatedamg(DamageValue) --value added to othervalue?
	if not DamageValue then warn("Axe has no damage value") return end

	local PlayerAxes = Player:WaitForChild("axes", 5)
	if not PlayerAxes then warn("Player has no axes") return end

	local twxWood = PlayerAxes:FindFirstChild("twxWood", 5)

	print(DamageValue)
	wood.Value += DamageValue * (twxWood.Value and 2 or 1) -- Multiply by 2 if twxWood, otherwise 1
	DamageValue = 0
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

The parameters or arguments are just values passed into the function. They can have almost any name.

local function foo(arg1, arg2, arg3)
	return arg1 + arg2 - arg3
end

print(foo(1,2,3))

Output: 0

Hw would I use paramaters in mu situation? And where would paramters be good to use in a different code?

My advice would be to read the documentation on functions.

You pass in values of any kind into the function call. From there the function now has access to those values.

ok so i would do something like this??

num1 = 0

local function add(num1)
      num1 = 2
end

script.Parent.Mousebutton1click:Connect(Function()
       num1 = add()
end)

Close.

You want to put the parameter inside the function call.

add(num1)

Ok so when i impliment this into my code it should work like this right? But it is warning “axe has no damage value”?

local function updatedamg(idk)
	local Character = plr.Character or plr.CharacterAdded:Wait()
	local Axe = SearchForAxe(Character)
	local idk =	Axe:FindFirstChild("damage").Value --finding the value
	print(idk)
end

AddWoodEvent.OnServerEvent:Connect(function(Player) -- (A) Were you supposed to add "Wood" as a parameter
	DamageValue = updatedamg(DamageValue) --add dmg
	if not DamageValue then warn("Axe has no damage value") return end

	local PlayerAxes = Player:WaitForChild("axes", 5)
	if not PlayerAxes then warn("Player has no axes") return end

	local twxWood = PlayerAxes:FindFirstChild("twxWood", 5)

	print(DamageValue)
	wood.Value += DamageValue * (twxWood.Value and 2 or 1) -- Multiply by 2 if twxWood, otherwise 1
	DamageValue = 0
end)

When you pass variables to a subprogram, the value is passed - not the variable itself.

--quick example
local function something(parameter1)
    print(parameter1)
end

something(3) --outputs "3"

local var = 5
something(var) --outputs "5"

Basically, parametes are ways of passing useful informations to sub-programs that they can then use. Sorry this explanation is so brief.


Here’s a post I made about parameters:
Parameters

Here’s the documentation:
Using Parameters and Events | Documentation - Roblox Creator Hub

I hope these resources help. You are, in fact, already using parameters, for example in FindFirstChild!

1 Like

Ok, i understand more now, but i think im setiting “damage value” to idk’s value. But damage value is printing nil?

local function updatedamg(idk: NumberValue) 
	local Character = plr.Character or plr.CharacterAdded:Wait()
	local Axe = SearchForAxe(Character)
	local idk =	Axe:FindFirstChild("damage").Value --getting idks value
	print(idk)
end

AddWoodEvent.OnServerEvent:Connect(function(Player)
	DamageValue = updatedamg(DamageValue) --setting the damage value to idks value
	print(DamageValue)
	if not DamageValue then warn("Axe has no damage value") return end

	local PlayerAxes = Player:WaitForChild("axes", 5)
	if not PlayerAxes then warn("Player has no axes") return end

	local twxWood = PlayerAxes:FindFirstChild("twxWood", 5)

	print(DamageValue)
	wood.Value += DamageValue * (twxWood.Value and 2 or 1) 
	DamageValue = 0
end)

DamageValue is never being set in that script.

You have made what is called a procedure, it does not return a value. This means DamageValue comes out as nil, which means the asbence of data (e.g. nothing). You must add a return statement returning the value you want DamageValue to take.

damage value is higher up in the code

local DamageValue = 0 --setting damage vllue

local function SearchForAxe(Container) -- Move local function outside of connection
	for _, Item in Container:GetChildren() do
		if not Item:IsA("Tool") or Item.Name ~= "Axe" then continue end
		return Item
	end
end

local function updatedamg(idk: NumberValue) 
	local Character = plr.Character or plr.CharacterAdded:Wait()
	local Axe = SearchForAxe(Character)
	local idk =	Axe:FindFirstChild("damage").Value
	print(idk)
end

AddWoodEvent.OnServerEvent:Connect(function(Player)
	DamageValue = updatedamg(DamageValue)
	print(DamageValue)
	if not DamageValue then warn("Axe has no damage value") return end

	local PlayerAxes = Player:WaitForChild("axes", 5)
	if not PlayerAxes then warn("Player has no axes") return end

	local twxWood = PlayerAxes:FindFirstChild("twxWood", 5)

	print(DamageValue)
	wood.Value += DamageValue * (twxWood.Value and 2 or 1) 
	DamageValue = 0
end)

Like I said, this never returns a value, which means DamageValue will end up as nil (nothing).

Also try to avoid global variables to prevent future issues.

so how would i make it return a value

Simple: You just add a return statement to return the value you want. Here’s a brief example using addition:

local function add(n1, n2)
    local result = n1 + n2
    return result --send it back to whatever requested it
end

local sum = add(5, 7) --get the return result of the function and store it in "sum"
print(sum) --outputs "12"
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.