Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Hello new here, my slingshot isn't working properly, someone please help me?

Ok so I have a FE compatible slingshot but the pellets are launching the wrong way when I click, here are the scripts:

local debris = game:service("Debris")
pellet = script.Parent
damage = 8

function tagHumanoid(humanoid)
	local tag = pellet:findFirstChild("creator")
	if tag ~= nil then
		while(humanoid:findFirstChild("creator") ~= nil) do
			humanoid:findFirstChild("creator").Parent = nil
		end

		local new_tag = tag:clone()
		new_tag.Parent = humanoid
		debris:AddItem(new_tag, 1)
	end
end

function onTouched(hit)
	local hitChar = hit:FindFirstAncestorWhichIsA("Model")
	local humanoid = hitChar:FindFirstChild("Humanoid")
	if humanoid ~= nil then
		local canDamage = true
		local tag = pellet:FindFirstChild("creator")
		if tag then
			local creator = tag.Value
			local player = game.Players:GetPlayerFromCharacter(hitChar)
			if creator and player then
				if creator == player or (creator.Team and player.Team and creator.Team == player.Team) then
					canDamage = false
				end
			end
		end
		if canDamage then
			tagHumanoid(humanoid)
			humanoid:TakeDamage(damage)
		end
	else
		damage = damage / 2
		if damage < 1 then
			connection:disconnect()
			pellet.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

pellet.Parent = nil

Tool = script.Parent VELOCITY = 85

local Pellet = Instance.new("Part") Pellet.Locked = true Pellet.BottomSurface = 0 Pellet.TopSurface = 0 Pellet.Shape = 0 Pellet.Size = Vector3.new(1,1,1) Pellet.BrickColor = BrickColor.new(2)

Tool.PelletScript:clone().Parent = Pellet

function spawnSound(sound) local s = sound:clone() s.Parent = sound.Parent s:Play() s.Ended:connect(function () s:Destroy() end) end

function fire(mouse_pos)

spawnSound(Tool.Handle.SlingshotSound)

local vCharacter = Tool.Parent
local vPlayer = game.Players:playerFromCharacter(vCharacter)

local head = vCharacter: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

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 = Pellet:clone()
    

	

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

missile.PelletScript.Disabled = false

local creator_tag = Instance.new("ObjectValue")
creator_tag.Value = vPlayer
creator_tag.Name = "creator"
creator_tag.Parent = missile

missile.Parent = game.Workspace
missile:SetNetworkOwner(vPlayer)

end

function computeLaunchAngle(dx,dy,grav)

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

Tool.Enabled = true function onActivated()

if not Tool.Enabled then
	return
end

Tool.Enabled = false

local character = Tool.Parent;
local humanoid = character.Humanoid
if humanoid == nil then
	print("Humanoid not found")
	return 
end

if humanoid.Health <= 0 then
	return
end

local targetPos = humanoid.TargetPoint

fire(targetPos)

wait(.2)

Tool.Enabled = true

end

script.Parent.Activated:connect(onActivated)


2 answers

I know this may not help but your could try out this Slingshot free model I found.

It was updated a couple of months ago so it is recent. It also works with FE and R15

https://www.roblox.com/library/590732086/Slingshot

4 comments

0

yeah tried that it works the same as mine it keeps going the wrong way, so I try clicking somewhere for where it should go then it goes the wrong way like last time, in the studio client it works perfectly, but in server it launches pellets the wrong way, What now?

0

Ive got no clue then, sorry man.

0

Well there is a game called Crossroads FE and when I go in game the slingshot works excellent but when I get the slingshot from the game and put it in my game the slingshot still launches pellets the wrong way here is game link: https://web.roblox.com/games/1047878118/Crossroads-FE

0

Im sorry man I just dont know, I am not the best scripter. Im new to all this too.

This discussion is preserved as it was. New answers, comments, and votes are closed.