Script math problem

Hello,

I’m trying to do some math, but I got stuck

I’m trying to make a path to the player with a set block number, like lets say I did 10 blocks it would make a path with only 10 blocks to me and the parent_part along with that it would also go up and down I don’t know if I was close or not

script:

local player_service = game:GetService("Players")
local run_service = game:GetService("RunService")
local work_space = game:GetService("Workspace")

local parent_part = script.Parent

function on_player_enter(player: Player)
	player.CharacterAppearanceLoaded:Connect(function()
		
		run_service.Heartbeat:Connect(function()
			
			local unit = (player.Character.HumanoidRootPart.Position - parent_part.Position).Unit
			local distance = (player.Character.HumanoidRootPart.Position - parent_part.Position).Magnitude
		
			local steps = (distance / 2)
			
			for i = 1, steps - 1 do
				local forward_position = distance * parent_part.Position
				
				local up_down_position = distance * math.sin(steps * distance)
				
				local combied_position = forward_position * Vector3.new(0, up_down_position, 0)
				
				local part = Instance.new("Part")
				part.Position = combied_position
				part.Size = Vector3.new(1, 1, 1)
				part.Anchored = true
				part.Parent = work_space
				
			end
			
		end)
		
	end)
end

player_service.PlayerAdded:Connect(on_player_enter)

This will make the up and down movement based on distance squared which I don’t think is what you want:

local up_down_position = distance * math.sin(steps * distance)

I think this is supposed to be doing a lerp based on your description. I assume each block is two studs long based on the steps calculation. If so it should be like this:

local forward_position = parent_part.Position + unit * i * 2

This should almost certainly be addition instead of multiplication:

local combied_position = forward_position + Vector3.new(0, up_down_position, 0)

If you can, draw or build an example of what is supposed to be generated.

1 Like

I already got it working, it took a lot of errors and retrying, but I got it.

Script for those who run into this problem:

local player_service = game:GetService("Players")
local run_service = game:GetService("RunService")
local work_space = game:GetService("Workspace")
local debris = game:GetService("Debris")

local parent_part = script.Parent

function create_part(target_position)
	local part = Instance.new("Part")
	part.Position = target_position
	part.Size = Vector3.new(1, 1, 1)
	part.Anchored = true
	part.Color = Color3.fromRGB(255)
	part.Transparency = 0.5
	part.CanCollide = false
	part.Parent = work_space
	debris:AddItem(part, 0.05)
end

local function create_path(Power, Uplevel, Amount, A, B)
	local unit = (B - A).Unit
	local distance = (B - A).Magnitude
	
	local steps = distance / (Amount - 1)
	
	for i = 1, Amount - 1 do
		
		local forward_position = A + unit * (i * steps)
		
		local jump_position = Uplevel * math.sin(i * Power)
		
		local right_position = Uplevel * math.cos(i * Power)
		
		local left_position = Uplevel * math.cos(i * Power)
		
		local new_position = forward_position + Vector3.new(right_position, jump_position, left_position)
		
		create_part(new_position)
		
	end
	
end

function on_player_enter(player: Player)
	player.CharacterAppearanceLoaded:Connect(function()
		
		run_service.Heartbeat:Connect(function()
			
			if player then
				if player.Character then
					if player.Character:FindFirstChild("HumanoidRootPart") then
						create_path(0.6, 2, 900, parent_part.Position, player.Character.HumanoidRootPart.Position)
					end
				end
			end
			
		end)
		
	end)
end

player_service.PlayerAdded:Connect(on_player_enter)
1 Like