#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)
is this a
It is not
local Character = script.Parent.Parent
local Player = game.Players:GetPlayerFromCharacter(Character)
local ActivatedUserID = Player and Player.UserId
try replacing this with the player
Not an expert but are you looking for damage?? I can’t see anything that would cause damage, it just prints the players name
Still doesn't work in local 2 player server
are you checking console or f9 console?
That is the goal, but I haven't gotten to that part yet. It prints the player's name when I just hit play, but not when I run a local server
If console is at the bottom, and f9 console is on the screen, then I am checking both
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??
I noticed lol, I tried asking ai for help before coming here
Yes, I did that while debugging. It prints hit player and the player's userid when I hit play, but not in a local 2 player server
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)