#Trying to make a knife (that hurts players) but I can't get it to work in a local 2 player server

1 messages · Page 1 of 1 (latest)

warm mirage
#

Whenever I run my script while just hitting play, it works as intended. However, once I switch over to a 2 player local server it doesn't work. Also, it is in a normal script.

warm mirage
#

It is not

rocky geyser
#

mbmb

#

hollon

warm mirage
#

There is some more info on how it is set up

rocky geyser
#
local Character = script.Parent.Parent
local Player = game.Players:GetPlayerFromCharacter(Character)
local ActivatedUserID = Player and Player.UserId
#

try replacing this with the player

polar marsh
#

Not an expert but are you looking for damage?? I can’t see anything that would cause damage, it just prints the players name

warm mirage
rocky geyser
warm mirage
warm mirage
polar marsh
#

Of course! I can help you with your Roblox script. It's a common issue, and you're very close to getting it right. The main problem is that the core logic for checking the hit player is commented out, and there are a few other improvements we can make to ensure it's reliable and doesn't cause lag.

Here is the corrected and improved version of your script. I recommend replacing your current script with this one.

Corrected Script

#

Your script's primary issue was that the core logic was commented out, so it never checked if a different player was hit.

Key fixes in the new script:

Enabled Logic: The check to see if the hitPlayer is different from the attacker is now active.

Reliable Player Reference: Instead of the fragile script.Parent.Parent.Parent, it uses game.Players.LocalPlayer inside a LocalScript, which is the correct and robust method.

Hitbox Cleanup: The original script never deleted hitboxes, which would cause extreme lag over time. The new script uses the Debris service to automatically remove the hitbox after 0.3 seconds, keeping the game running smoothly.

Debounce: A debounce variable was added to prevent players from spamming the attack and creating multiple hitboxes, ensuring one clean swing at a time.

Corrected Checks: The if statement now properly confirms the target is a valid player (if hitPlayer) and not the attacker (hitPlayer ~= Player)

#

Wait

#

That was all ai if u couldn’t tell

#

But did you mean to comment out

#

That part??

warm mirage
#

I noticed lol, I tried asking ai for help before coming here

warm mirage
#
 
local Knife = script.Parent
local Character = script.Parent.Parent
local Player = game.Players:GetPlayerFromCharacter(Character)
local ActivatedUserID = Player and Player.UserId

Knife.Activated:Connect(function()
    local KnifeHitBox = game.ReplicatedStorage.KnifeHitBox:Clone()
    local KnifeHitboxPOS = Knife.Handle.CFrame
    
    KnifeHitBox.CFrame = KnifeHitboxPOS
    KnifeHitBox.Parent = game.Workspace
    
    --Check to see if the knife touches a player
    KnifeHitBox.Touched:Connect(function(hit) --checks to see if part touching hitbox is player
            if hit.Parent:FindFirstChild("Humanoid") then
                print("HitPlayer")
                local hitplayer = game.Players:GetPlayerFromCharacter(hit.Parent)
                print(hitplayer.UserId)
                --if Player.UserId ~= ActivatedUserID then --checks if player is the player that swung the knife
                --    print("different Player")
                --end
            end
    end)
end)