#how do i make it so i can throw a knife

1 messages · Page 1 of 1 (latest)

green karma
#

pls tell me if u can script this

fathom mango
#

with code and vectors and probably a knife tool Thumbs

primal gyro
# green karma pls tell me if u can script this

Local script inside of your knife tool:

local tool = script.Parent
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local rs = game:GetService("ReplicatedStorage")
local event = rs:WaitForChild("ThrowKnifeEvent")
local canThrow = true
local cooldown = 0.35

tool.Activated:Connect(function()
    if not canThrow then return end
    canThrow = false
    local origin = (tool:FindFirstChild("Handle") and tool.Handle.Position) or (player.Character and player.Character:FindFirstChild("HumanoidRootPart") and player.Character.HumanoidRootPart.Position)
    if not origin then
        canThrow = true
        return
    end
    local target = mouse.Hit.p
    local dir = (target - origin)
    if dir.Magnitude == 0 then dir = player.Character.HumanoidRootPart.CFrame.LookVector end
    dir = dir.Unit
    event:FireServer(origin, dir)
    wait(cooldown)
    canThrow = true
end)

Serverscriptservice:

local rs = game:GetService("ReplicatedStorage")
local throwEvent = rs:FindFirstChild("ThrowKnifeEvent") or Instance.new("RemoteEvent", rs)
throwEvent.Name = "ThrowKnifeEvent"
local Debris = game:GetService("Debris")
local SPEED = 180
local DAMAGE = 25
local LIFETIME = 6

throwEvent.OnServerEvent:Connect(function(player, origin, direction)
    if not player or not origin or not direction then return end
    local knife = Instance.new("Part")
    knife.Size = Vector3.new(1, 0.2, 0.2)
    knife.CFrame = CFrame.new(origin, origin + direction) * CFrame.new(0, 0, -1.5)
    knife.CanCollide = false
    knife.Anchored = false
    knife.Massless = true
    knife.Name = "KnifeProjectile"
    knife:SetAttribute("OwnerUserId", player.UserId)
    knife.Parent = workspace
    knife.AssemblyLinearVelocity = direction * SPEED
    knife.TopSurface = Enum.SurfaceType.Smooth
    knife.BottomSurface = Enum.SurfaceType.Smooth
    local hitOnce = false
    knife.Touched:Connect(function(hit)
        if hitOnce then return end
        hitOnce = true
        local model = hit:FindFirstAncestorOfClass("Model")
        local hum
        if model then hum = model:FindFirstChildWhichIsA("Humanoid") end
        if not hum then
            local maybeHum = hit.Parent and hit.Parent:FindFirstChildWhichIsA("Humanoid")
            hum = maybeHum or hum
        end
        if hum and hum.Health > 0 then
            local victimChar = hum.Parent
            local victimPlayer = game.Players:GetPlayerFromCharacter(victimChar)
            local ownerId = knife:GetAttribute("OwnerUserId")
            if not victimPlayer or victimPlayer.UserId ~= ownerId then
                hum:TakeDamage(DAMAGE)
                knife:Destroy()
                return
            end
        end
        knife:Destroy()
    end)
    Debris:AddItem(knife, LIFETIME)
end)
#

Make a remote event in replicatedstorage called ThrowKnifeEvent

#

Make sure the knife is in starterpack and is called Knife

#

You should be able to grab a knife out of the toolbox

primal gyro
upper berry
primal gyro
upper berry
#

they just gonna come back asking for it again

#

no

#

its not even allowed to ask

primal gyro
#

oh mb

#

it did take me some time to make

upper berry
upper berry
primal gyro
#

thanks Thumbs

lethal copper
#

a better way to help someone is give them steps and hints to solve the issue on their own, feed a man once and he’ll be hungry, teach a man to fish and he’ll never be hungry

green karma
#

Thats chat gpt code

green karma
primal gyro
#

Chatgpt is decent at coding

#

so am i

#

pretty immature to say don’t you think

lethal copper
# green karma Thats chat gpt code

lmao it’s literally not. ai won’t have variables like ”rs” and ”event”. there’s also no ai artifacts in the code.

lethal copper
# upper berry why shouldn't it?

it doesn’t help people learn, only provides them with a one off solution rather than experience needed to learn more and possibly solve problems on their own later

#

it’s a short term solution to a long term problem

upper berry