Variable doesnt change in time from module

hello

im trying to change a variable from a module right before it’s called but the variable doesnt change intime and uses the old variable

--calling variable change
ViewModelFramework.Offset(Player, MissDirection)
		
--when variables needed
		MissDirection = MissDirection + Vector3.new(math.random(-SprayOffset, SprayOffset), math.random(-SprayOffset,SprayOffset),0)

is there anyway to still use a module and not wait too long for the variable change

3 Likes

What is MissDirection exactly? What does ViewModelFramework.Offset do? Can you show some more code?

missdirection is the direction of the raycast, it goes to your mouse but should be changed from the offset module but since am calling the variable right after the modules called i think the module doesn’t get enough time to change the variable and it uses the old one

–viewmodel.offset

function ViewModelFramework.Offset(Player, OriginalDirection)
	local BodyVelocity = Player.Character.Torso.Velocity.magnitude
	
	local SprayOffset = BodyVelocity * 0.7
	
	print(SprayOffset)
	
	print(OriginalDirection,"Original")
	
	OriginalDirection = OriginalDirection + Vector3.new(math.random(-SprayOffset, SprayOffset), math.random(-SprayOffset,SprayOffset),0)
	
	print(OriginalDirection, "Original, new")
	
	
	
end
1 Like

Can’t you just return the updated variable from the module function call?

function ViewModelFramework.Offset(Player, OriginalDirection)
	local BodyVelocity = Player.Character.Torso.Velocity.magnitude

	local SprayOffset = BodyVelocity * 0.7

	print(SprayOffset)

	print(OriginalDirection,"Original")

	OriginalDirection = OriginalDirection + Vector3.new(math.random(-SprayOffset, SprayOffset), math.random(-SprayOffset,SprayOffset),0)

	print(OriginalDirection, "Original, new")

	return OriginalDirection

end



--calling variable change
MissDirection=ViewModelFramework.Offset(Player, MissDirection)

--when variables needed
MissDirection = MissDirection + Vector3.new(math.random(-SprayOffset, SprayOffset), math.random(-SprayOffset,SprayOffset),0)

yeah, I agree with Zombie. Why doesn’t this return the value? If you’re changing a variable from the ModuleScript, calling it from other scripts won’t do anything.

i wrote the example wrong in first post it goes like this

ViewModelFramework.Offset(Player, MissDirection)

local MissRay =  workspace:Raycast(Start, MissDirection, MissParams)

i dont think it gets enough time to change the value because the module takes too long before changing it but idk

The module isn’t changing anything except a variable in the module. You need it to return the value and then set that to a variable.

Cause it isn’t changing it

1 Like

can u write an example pls? not sure what you mean

So, in the module, you have OriginalDirection, and you change it with the Vector3. However, you aren’t changing OriginalDirection in the other script. When you pass it as a parameter, you’re only passing the value, not the reference. Changing it on the module doesn’t do anything. What you should be doing is

return OriginalDirection + Vector3.new(math.random(-SprayOffset, SprayOffset), math.random(-SprayOffset,SprayOffset),0)

and then

MissDirection = ViewModelFramework.Offset(Player, MissDirection)
1 Like

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