How do I do this?

I have a script that lets me know when the player presses E. But how would I make it so that when the player actually presses E, it throws something at the cursor. Like if you were to throw a grappling hook. I know when the player presses E. How and where do I even start?

1 Like
2 Likes

I’ll watch it, thanks for the video!

1 Like

This is very helpful, but I need it to go to my mouse cursor not just straight forward. How do I do that? Also is there any way to give the projectile gravity?

I believe Player:GetMouse().Hit.p returns the position that your cursor is touching.
You can get that position and send an object to that position, or at least in its general direction

This is the current script:

local event = script.Parent.EPressed
local replicatedStorage = game:GetService("ReplicatedStorage")
local HookModel = replicatedStorage.Hook

event.OnServerEvent:Connect(function(Player)
	print("Fired to script")
	local char = script.Parent
	local HookProjectile = HookModel:Clone("Hook", char)
	HookProjectile.Parent = workspace
	HookProjectile.Anchored = false
	HookProjectile.CanCollide = false
	HookProjectile.CFrame = char.HumanoidRootPart.CFrame
	
	
	local bv = Instance.new("BodyVelocity", HookProjectile)
	
	
	
	bv.MaxForce = Vector3.one * math.huge
	bv.Velocity = char.HumanoidRootPart.CFrame.LookVector * 40
	game.Debris:AddItem(HookProjectile, 2)
	
end)

I tried doing Player:GetMouse().Hit.p but it said “Attempt to index hit with nil”
(I removed the GetMouse() for now)

Try doing local Mouse = player:GetMouse() and then doing local Hit = mouse.Hit.p

Did this:

local event = script.Parent.EPressed
local replicatedStorage = game:GetService("ReplicatedStorage")
local HookModel = replicatedStorage.Hook.Union

event.OnServerEvent:Connect(function(Player)
	print("Fired to script")
	local char = script.Parent
	local HookProjectile = HookModel:Clone("Hook", char)
	HookProjectile.Parent = workspace
	HookProjectile.Anchored = false
	HookProjectile.CanCollide = false
	HookProjectile.CFrame = char.HumanoidRootPart.CFrame
	
	
	local bv = Instance.new("BodyVelocity", HookProjectile)
	local Mouse = Player:GetMouse()
	local Hit = Mouse.Hit.p

	bv.MaxForce = Vector3.one * math.huge
	bv.Velocity = Hit * 50
	--bv.Velocity = char.HumanoidRootPart.CFrame.LookVector * 20
	game.Debris:AddItem(HookProjectile, 2)
	
end)

And still getting the error: Attempt to index nil with “Hit”

It could be an issue with using a normal instead of local script. Can you try getting the position using Player:GetMouse() and hit on the local script and then sending that through the remote event to the normal script?

Did that and now it says:
“attempt to perform arithmetic (mul) on instance and number.”

script:

local event = script.Parent.EPressed
local replicatedStorage = game:GetService("ReplicatedStorage")
local HookModel = replicatedStorage.Hook.Union

event.OnServerEvent:Connect(function(Player, Hit)
	print("Fired to script")
	local char = script.Parent
	local HookProjectile = HookModel:Clone("Hook", char)
	HookProjectile.Parent = workspace
	HookProjectile.Anchored = false
	HookProjectile.CanCollide = false
	HookProjectile.CFrame = char.HumanoidRootPart.CFrame
	
	
	local bv = Instance.new("BodyVelocity", HookProjectile)
	

	bv.MaxForce = Vector3.one * math.huge
	bv.Velocity = Hit * 50
	
	--bv.Velocity = char.HumanoidRootPart.CFrame.LookVector * 20
	game.Debris:AddItem(HookProjectile, 2)
	
end)

local script:

local uis = game:GetService("UserInputService")
local DB = false

uis.InputBegan:Connect(function(input, istyping)
	if istyping then return end
		if input.KeyCode == Enum.KeyCode.E then
		if DB == false then
			DB = true
			print("Pressed E")
			local Player = game.Players.LocalPlayer
			local Mouse = Player:GetMouse()
			local Hit = Mouse.Hit.Position
			script.Parent.EPressed:FireServer(Player, Hit)
			
			wait(1)
			DB = false
		end
	end
end)

Try doing
bv.Velocity = tonumber(Hit) * 50

still getting the same stupid error.

Fixed it, instead of using bv’s and all that I decided to use CFrames, it probably works better as a CFrame for my future plans anyway.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.