Skip to content

Instantly share code, notes, and snippets.

@Sleitnick
Created November 7, 2018 15:47
Show Gist options
  • Save Sleitnick/b1bbd0a6863770f009f81ff784da3d68 to your computer and use it in GitHub Desktop.
Save Sleitnick/b1bbd0a6863770f009f81ff784da3d68 to your computer and use it in GitHub Desktop.
Platform creator
-- Platform
-- Crazyman32
-- November 6, 2018
--[[
This should be a LocalScript within StarterCharacterScripts.
This creates platform parts as the player runs around. It does
not replicate the platforms to other players. So only the local
player will see his/her platform, and will not see the platform
of other players.
This was created just for fun. Feel free to use it however you
would like. I hope it serves a good educational purpose.
--]]
local root = script.Parent:WaitForChild("HumanoidRootPart")
local platforms = {}
local rng = Random.new()
-- Y position of the platform:
local POS_Y = 20
local FADE_IN_DURATION = 0.25
local FADE_OUT_DURATION = 0.50
-- Platform part:
local platformPrefab = Instance.new("Part")
platformPrefab.Name = "Platform"
platformPrefab.Anchored = true
platformPrefab.Locked = true
platformPrefab.TopSurface = Enum.SurfaceType.Smooth
platformPrefab.BottomSurface = Enum.SurfaceType.Smooth
platformPrefab.Material = Enum.Material.Grass
platformPrefab.Color = Color3.fromRGB(138, 171, 133)
platformPrefab.Size = Vector3.new(16, 8, 16)
local platformSize = platformPrefab.Size.X
-- Folder in which platforms go into:
local platformFolder = workspace:FindFirstChild("Platforms")
if (platformFolder) then
platformFolder:ClearAllChildren()
else
platformFolder = Instance.new("Folder")
platformFolder.Name = "Platforms"
platformFolder.Parent = workspace
end
local function Round(x, mult)
return math.floor((x / mult) + 0.5) * mult
end
local function FadeIn(part, callback)
local info = TweenInfo.new(FADE_IN_DURATION, Enum.EasingStyle.Quint, Enum.EasingDirection.Out)
local cfEnd = part.CFrame
local cfStart = cfEnd + Vector3.new(rng:NextNumber(-40, 40), -10, rng:NextNumber(-40, 40))
part.CFrame = cfStart
local tween = game:GetService("TweenService"):Create(part, info, {
Transparency = 0;
CFrame = cfEnd;
})
tween.Completed:Connect(callback)
tween:Play()
end
local function FadeOut(part, callback)
local info = TweenInfo.new(FADE_OUT_DURATION, Enum.EasingStyle.Quint, Enum.EasingDirection.In)
local tween = game:GetService("TweenService"):Create(part, info, {
Transparency = 1;
CFrame = part.CFrame + Vector3.new(0, -10, 0);
})
tween.Completed:Connect(callback)
tween:Play()
end
-- Create and manage a single platform piece:
local function CreatePlatform(pos)
local key = tostring(pos.X) .. ":" .. tostring(pos.Z)
if (platforms[key]) then return end
platforms[key] = true
local platform = platformPrefab:Clone()
platform.CFrame = CFrame.new(pos)
platform.Transparency = 1
platform.Parent = platformFolder
local temp = platform:Clone()
temp.Parent = platformFolder
FadeIn(platform, function()
temp:Destroy()
local heartbeat
local removalDistance = (platformSize * 2)
-- Wait for part to be too far away, and then fade it out:
heartbeat = game:GetService("RunService").Heartbeat:Connect(function()
if ((platform.Position - root.Position).Magnitude > removalDistance) then
heartbeat:Disconnect()
platforms[key] = nil
FadeOut(platform, function()
platform:Destroy()
end)
end
end)
end)
end
-- Run step each frame:
local function Step()
-- Project forward based on the movement velocity (if not moving, just use direction player is facing)
local unit = (root.Velocity.Magnitude < 0.01 and root.CFrame.LookVector or root.Velocity.Unit)
local projectedPos = root.Position + (unit * (platformSize * 1.5))
-- Round projected position to the size of the platform:
local x = Round(projectedPos.X, platformSize)
local z = Round(projectedPos.Z, platformSize)
local pos = Vector3.new(x, POS_Y, z)
-- Create platform at current player position AND projected next position:
CreatePlatform(Vector3.new(Round(root.Position.X, platformSize), POS_Y, Round(root.Position.Z, platformSize)))
CreatePlatform(pos)
end
game:GetService("RunService").Heartbeat:Connect(Step)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment