Hello guys. I’m trying to rescript Roblox controls modules, and add part swimming system. But I run into one problem:
.Initialize creates self
, with some values, and that values aren’t visible to diffirent parts of script:
Example:
I .initialize module, and then I try to swim. AlignOrientation/self.DirectionalForce are created, but can’t be found with :GetDirectionalForce function.
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local Folder = workspace.WaterFolder
local WaterDetector = {}
WaterDetector.__index = WaterDetector
local WaterDetectionParams = OverlapParams.new()
WaterDetectionParams.FilterType = Enum.RaycastFilterType.Include
WaterDetectionParams.BruteForceAllSlow = true
function WaterDetector.Initialize()
local InitialInfo = {
CurrentCharacter = nil,
CurrentHumanoid = nil,
InsideWater = false,
AntiGravForce = nil,
DirectionalForce = nil,
WaterDetectionFunction = nil,
WaterParts = {},
}
local self = setmetatable(InitialInfo, WaterDetector)
Players.LocalPlayer.CharacterAdded:Connect(function(Character) self:OnCharacterAdded(Character) end)
Players.LocalPlayer.CharacterRemoving:Connect(function(Character) self:OnCharacterRemoving() end)
if Players.LocalPlayer.Character then
self:OnCharacterAdded(Players.LocalPlayer.Character)
end
local HumRoot = self.CurrentCharacter and self.CurrentCharacter.HumanoidRootPart or nil
local WaterParts = Folder:GetChildren()
for i = 1, #WaterParts, 1 do
--Here, I unsert every water part into self.WaterParts
end
self.WaterDetectionFunction = RunService.RenderStepped:Connect(function(Delta)
if self.CurrentHumanoid then
local HumRoot = self.CurrentCharacter.HumanoidRootPart
local InsideWater = false
for WaterPart, Params in pairs(self.WaterParts) do
--My "Is in water" detection method
end
local HumValue = self.CurrentHumanoid.MovementState
if InsideWater then
if HumValue.Value ~= "Swim" then
self.CurrentHumanoid:Move(Vector3.zero, false)
self:ChangeHumanoidState(false, Enum.HumanoidStateType.Swimming)
local AntiGravity = Instance.new("VectorForce")
--Configuring this constraint with initial values.
AntiGravity.Parent = HumRoot
self.AntiGravForce = AntiGravity
local Rotator = Instance.new("AlignOrientation")
--Configuring this constraint with initial values.
Rotator.Parent = HumRoot
self.DirectionalForce = Rotator
warn(self.DirectionalForce) --HERE "AlighOrientation" exists
self.CurrentHumanoid.MovementState.Value = "Swim"
end
else
if HumValue.Value ~= "Walk" then
self:ChangeHumanoidState(true, Enum.HumanoidStateType.Running)
self.AntiGravForce:Destroy()
self.AntiGravForce = nil
self.DirectionalForce:Destroy()
self.DirectionalForce = nil
self.CurrentHumanoid.MovementState.Value = "Walk"
end
end
end
end)
return self
end
function WaterDetector:OnCharacterAdded(Character)
--Character adding
end
function WaterDetector:OnCharacterRemoving()
--Character removing
end
function WaterDetector:ChangeHumanoidState(Activate, ToSet)
self.CurrentHumanoid:SetStateEnabled(Enum.HumanoidStateType.Running, Activate)
self.CurrentHumanoid:SetStateEnabled(Enum.HumanoidStateType.RunningNoPhysics, Activate)
self.CurrentHumanoid:SetStateEnabled(Enum.HumanoidStateType.GettingUp, Activate)
self.CurrentHumanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, Activate)
self.CurrentHumanoid:SetStateEnabled(Enum.HumanoidStateType.Freefall, Activate)
self.CurrentHumanoid:SetStateEnabled(Enum.HumanoidStateType.FallingDown, Activate)
self.CurrentHumanoid:ChangeState(ToSet)
end
function WaterDetector:GetDirectionalForce()
print("Returning dirforce") -- prints
print(self.DirectionalForce) -- nil
return self.DirectionalForce -- nil
end
return WaterDetector
--requiring module
local module = WaterDetector.Initialize()
local Orientator = module:GetDirectionalForce() --nil, expected AlignOrientation
local Orientator2 = module.DirectionalForce -- nil again
Can someone tell me, why I’m running in this problem, and how I can fix it?