User:Eyegotbanned/Telenade Tutorial

From Roblox Wiki

Jump to: navigation, search

Create a hopperBin tool. Make it a script type. Put in it a script called "Slingshot" Insert the following code:

print("Slingshot script loaded")

bin = script.Parent
VELOCITY = 125 -- constant

function fire(mouse_pos)


	sound = Instance.new("Sound")
		sound.SoundId = "rbxasset://sounds\\Rubber band sling shot.wav"
		sound.Parent = script.Parent
		sound:play()

-- find player's head pos



	local head = game.Players.LocalPlayer.Character:findFirstChild("Head")
	if head == nil then return end

	local dir = mouse_pos - head.Position
	dir = computeDirection(dir)

	local launch = head.Position + 5 * dir

	local delta = mouse_pos - launch
	
	local dy = delta.y
	
	local new_delta = Vector3.new(delta.x, 0, delta.z)
	delta = new_delta

	local dx = delta.magnitude
	local unit_delta = delta.unit
	
	-- acceleration due to gravity in RBX units
	local g = (-9.81 * 20)

	local theta = computeLaunchAngle( dx, dy, g)

	local vy = math.sin(theta)
	local xz = math.cos(theta)
	local vx = unit_delta.x * xz
	local vz = unit_delta.z * xz
	

	local missile = script.Parent.Pellet:clone()
        

		

	missile.Position = launch
	missile.Velocity = Vector3.new(vx,vy,vz) * VELOCITY

	print("Pos X:", launch.x, "Y:", launch.y, "Z:", launch.z)
	print("Vel X:", vx, "Y:", vy, "Z:", vz)

	missile.PelletScript.Disabled = false

	local creator_tag = Instance.new("ObjectValue")
	creator_tag.Value = game.Players.LocalPlayer
	creator_tag.Name = "creator"
	creator_tag.Parent = missile
	
	missile.Parent = game.Workspace

end


function computeLaunchAngle(dx,dy,grav)
	-- arcane
	-- http://en.wikipedia.org/wiki/Trajectory_of_a_projectile
	
	local g = math.abs(grav)
	local inRoot = (VELOCITY*VELOCITY*VELOCITY*VELOCITY) - (g * ((g*dx*dx) + (2*dy*VELOCITY*VELOCITY)))
	if inRoot <= 0 then
		return .25 * math.pi
	end
	local root = math.sqrt(inRoot)
	local inATan1 = ((VELOCITY*VELOCITY) + root) / (g*dx)

	local inATan2 = ((VELOCITY*VELOCITY) - root) / (g*dx)
	local answer1 = math.atan(inATan1)
	local answer2 = math.atan(inATan2)
	if answer1 < answer2 then return answer1 end
	return answer2
end

function computeDirection(vec)
	local lenSquared = vec.magnitude * vec.magnitude
	local invSqrt = 1 / math.sqrt(lenSquared)
	return Vector3.new(vec.x * invSqrt, vec.y * invSqrt, vec.z * invSqrt)
end

enabled = true
function onButton1Down(mouse)
	if not enabled then
		return
	end

	enabled = false
	mouse.Icon = "rbxasset://textures\\GunWaitCursor.png"

	-- find the best cf
	--local cf = mouse.Hit
	--local v = cf.lookVector

	local pos = mouse.Hit.p

	fire(pos)

	wait(4)
	mouse.Icon = "rbxasset://textures\\GunCursor.png"
	enabled = true

end

function onSelected(mouse)
	print("slingshot selected")
	mouse.Icon = "rbxasset://textures\\GunCursor.png"
	mouse.Button1Down:connect(function() onButton1Down(mouse) end)
end

bin.Selected:connect(onSelected)

Then, create a brick named "Pellet" Make it a 1x1x1 sphere. Put a script into that called "PelletScript" and insert this code:


pellet = script.Parent
damage = 8

function stick(hit) 
	-- joint myself to the thing i hit 
	local weld = Instance.new("Weld") 

	weld.Part0 = pellet 
	weld.Part1 = hit 

	local HitPos = pellet.Position + (pellet.CFrame.lookVector * .5) 

	local CJ = CFrame.new(HitPos) 
	local C0 = pellet.CFrame:inverse() *CJ 
	local C1 = hit.CFrame:inverse() * CJ 

	weld.C0 = C0 
	weld.C1 = C1 

	weld.Parent = pellet 
end

function onTouched(hit)
	stick(hit)
	humanoid = hit.Parent:findFirstChild("Humanoid")

        wait(2)
        local boom = Instance.new("Explosion")
        boom.Position = script.Parent.Position
        boom.Parent = game.Workspace
        boom.BlastRadius = 8

	if humanoid~=nil then
		tagHumanoid(humanoid)
		humanoid.Health = humanoid.Health - damage
		wait(2)
		untagHumanoid(humanoid)
	else
		damage = damage / 2
		if damage < 0.1 then
			connection:disconnect()
		end
	end
end

function tagHumanoid(humanoid)
	-- todo: make tag expire
	local tag = pellet:findFirstChild("creator")
	if tag ~= nil then
		local new_tag = tag:clone()
		new_tag.Parent = humanoid
		
	end
end


function untagHumanoid(humanoid)
	if humanoid ~= nil then
		local tag = humanoid:findFirstChild("creator")
		if tag ~= nil then
			
			tag.Parent = nil
		end
	end
end

connection = pellet.Touched:connect(onTouched)

r = game:service("RunService")
t, s = r.Stepped:wait()
d = t + 2.0 - s
while t < d do
	t = r.Stepped:wait()
end

wait(0.5)
pellet.Parent = nil

There you have it, the real Telanade! A later version found by William, called the Tele-Nade, although it was the same as the original. Thanks to William for putting up most of this information about it.

Personal tools