Doesnt get local player hrp [solved]

Hello! new developer here, so these scripts are for the hitbox, but for some reason when i fire server, it gets all the players hrp instead of the player firing the server, please help!

(omg my English is terrible)

Server script

local rep = game:GetService("ReplicatedStorage")
local remotes = rep:WaitForChild("remotes")
local hitposition = nil

local Attack = remotes.Event
local hitting = remotes.goback
local green = false




Attack.OnServerEvent:Connect(function(plr, damage,x,y,z)
	hitting.OnServerEvent:Connect(function(plr,hitpos)
		hitposition = hitpos


	end)
	
	local part = Instance.new("Part")
	part.Parent = workspace
	part.Anchored = true
	part.CanCollide = false
	part.BrickColor = BrickColor.Red()
	part.Size = Vector3.new(x,y,z)
	part.CFrame = hitposition
	part.Material = "Neon"
	part.Transparency = 0.5
	part.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name ~= plr.Name and not green then
			hit.Parent:FindFirstChild("Humanoid"):TakeDamage(damage)
			green = true
			part.BrickColor = BrickColor.Green()
		end
	end)
	wait(0.1)
	game.Debris:AddItem(part,1)
	local part2 = Instance.new("Part")
	part.Parent = workspace
	part.Anchored = true
	part.CanCollide = false
	part.BrickColor = BrickColor.Red()
	part.Size = Vector3.new(x,y,z)
	part.CFrame = hitposition
	part.Material = "Neon"
	part.Transparency = 0.5
	part.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name ~= plr.Name and not green  then
			hit.Parent:FindFirstChild("Humanoid"):TakeDamage(damage)
			green = true
			part.BrickColor = BrickColor.Green()
		end
	end)
	wait(0.1)
	game.Debris:AddItem(part2,1)
	local part3 = Instance.new("Part")
	part.Parent = workspace
	part.Anchored = true
	part.CanCollide = false
	part.BrickColor = BrickColor.Red()
	part.Size = Vector3.new(x,y,z)
	part.CFrame = hitposition
	part.Material = "Neon"
	part.Transparency = 0.5
	part.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name ~= plr.Name and not green  then
			hit.Parent:FindFirstChild("Humanoid"):TakeDamage(damage)
			green = true
			part.BrickColor = BrickColor.Green()
		end
	end)
	wait(0.1)
	game.Debris:AddItem(part3,1)
	local part4 = Instance.new("Part")
	part.Parent = workspace
	part.Anchored = true
	part.CanCollide = false
	part.BrickColor = BrickColor.Red()
	part.Size = Vector3.new(x,y,z)
	part.CFrame = hitposition
	part.Material = "Neon"
	part.Transparency = 0.5
	part.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name ~= plr.Name and not green   then
			hit.Parent:FindFirstChild("Humanoid"):TakeDamage(damage)
			green = true
			part.BrickColor = BrickColor.Green()
		end
	end)
	wait(0.1)
	
	game.Debris:AddItem(part4,1)
	green = false
end)

local script

local play = game:GetService("Players")
local plr = play.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local rend = game:GetService("RunService")
local rep = game:GetService("ReplicatedStorage")
local uis = game:GetService("UserInputService")
local debri = game:GetService("Debris")
local teams = game:GetService("Teams").killer
plr.Team = teams



local qcd = false

local animator = hum:WaitForChild("Animator")


local hit = script:WaitForChild("attack")
local hitting = animator:LoadAnimation(hit)
local q = script:WaitForChild("q ability")
local locate = animator:LoadAnimation(q)


local debounce = false

local hitbox = rep.remotes:WaitForChild("Event")
local gottagofast = rep.remotes:WaitForChild("goback")


local Comp = require(rep.service.ModuleScript)
local tableUtils = require(rep.service.TableUtils)



local hitpos = {}

local damage = 10

local x = 5
local y = 5
local z = 7


uis.InputBegan:Connect(function(input,gpe)
	if gpe then return end
	local function gottafast()

		hitpos = plr.Character.HumanoidRootPart.CFrame * CFrame.new(0,0,-6)

		gottagofast:FireServer(hitpos)


	end
	rend.RenderStepped:Connect(gottafast)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then

	
		if debounce then return end
		hum.WalkSpeed *= 1.5
		debounce = true
		hitting:Play()
		
		
		
		hitbox:FireServer(damage, x, y, z, hitpos)
		
			
	
		wait(0.5)
		hum.WalkSpeed /= 1.5
		wait(0.4)
		debounce = false
		end
end)

uis.InputBegan:Connect(function(input,gpe)
	if gpe then return end
	if input.KeyCode == Enum.KeyCode.Q then
		

		if debounce then return end
		if qcd then return end
		hum.WalkSpeed = 0
		debounce = true
		locate:Play()
		for i, Player in pairs(play:GetPlayers()) do
			if Player ~= play.LocalPlayer then
				local Character = Player.Character

				if Character then
					if Character:FindFirstChildWhichIsA("Highlight") then
						Character:FindFirstChildWhichIsA("Highlight"):Destroy()
					end

					if Player.Team ~= play.LocalPlayer.Team then
						local Highlight = Instance.new("Highlight")
						Highlight.FillColor = Color3.new(1)
						Highlight.Parent = Character



		



		wait(0.9)
		
		qcd = true
		wait(3)
		game.Debris:AddItem(Highlight,1)
						debounce = false
					end
				end
			end
		end
	end
end)
local function cooldown()
	while qcd == true do
		wait(10)
		qcd = false
	end
end
rend.RenderStepped:Connect(cooldown)

Im pretty sure you are not meant to connect functions up to infinity to an event.
Also doing that is insanely unsafe+unoptimized+broken+unmaintainable and so on.

Also what does “hrp” stands for?
Hierarchy relative position?

RenderStepped,wait are outdated btw

also

local function cooldown()
	while qcd == true do
		wait(10)
		qcd = false
	end
end
rend.RenderStepped:Connect(cooldown)

That literally a lag machine :skull:

Its not possible to solve the issue when code sabotages itself every frame :skull:

1 Like

Hello there ;]

Adding to what yarik said
Your code is creating connections to seek events where it shouldn’t.

This piece of code is basically hearing for an attack. Once it actvates, it creates a new connection that seeks for a “hitting” event. This connection is created every attack, so once the hit event fires, it would activate lots of times

This piece is firing the function every frame, so everey frame it starts a new cooldown. This generates a lot of work for the computer.

To solve this part you should use task.spawn(Cooldown), so it runs the code “in parallel” and only once when you call it

Im kinda new to programming too, but with practice and learning you’ll get the hang of it ;]

@Yarik_superpro I think hrp means Humanoid Root Part ;]

task.spawn runs code in serial (in this context), not parallel
it just runs code on a different thread.

1 Like

im new to coding :sob:
i only started 2 months ago xd

so like how would i be able to fix this?

lemme help you with your lil’ hitboxy script :sparkles::paw_prints: (also your English is fine!! like fr you’re doing great omg :sparkling_heart:)

⸝

SOOO what’s going on here:
You’re accidentally stacking the hitting.OnServerEvent:Connect(…) over and over every time someone attacks! :scream: That means it’s just like… duplicating itself and going “what player?? idk EVERYONE!!” and grabbing all the player HRPs like a confused spaghetti noodle :spaghetti:

That’s why it’s not targeting the right person — the code doesn’t know which position came from which player anymore :sob:

⸝

:dart: How to fix this:

You wanna keep track of each player’s hit position in a nice little organized way (like labeled jars of jam in a codey pantry :strawberry::package::sparkles:)

:bulb: Here’s a cleaned-up version:

local rep = game:GetService("ReplicatedStorage")
local remotes = rep:WaitForChild("remotes")
local Attack = remotes.Event
local hitting = remotes.goback

local playerHitPositions = {}

-- when someone says "hey here's where I hit!!"
hitting.OnServerEvent:Connect(function(player, hitPosition)
	playerHitPositions[player] = hitPosition
end)

-- when someone says "I ATTACKED!! pewpew!!"
Attack.OnServerEvent:Connect(function(player, damage, x, y, z)
	local hitPos = playerHitPositions[player]
	if not hitPos then
		warn("No hit position for", player.Name, "!!! 👉😢👈")
		return
	end

	for i = 1, 4 do
		local part = Instance.new("Part")
		part.Size = Vector3.new(x, y, z)
		part.CFrame = hitPos
		part.Anchored = true
		part.CanCollide = false
		part.BrickColor = BrickColor.Red()
		part.Material = Enum.Material.Neon
		part.Transparency = 0.5
		part.Parent = workspace

		local hitOnce = false
		part.Touched:Connect(function(hit)
			local hum = hit.Parent:FindFirstChild("Humanoid")
			if hum and hit.Parent.Name ~= player.Name and not hitOnce then
				hum:TakeDamage(damage)
				hitOnce = true
				part.BrickColor = BrickColor.Green()
			end
		end)

		game.Debris:AddItem(part, 1)
		wait(0.1)
	end

	playerHitPositions[player] = nil -- clean up afterwards ✨
end)

:brain: TL;DR:
• Don’t nest OnServerEvent inside another one!!! Bad. messy. chaotic. :bug::boom:
• Keep hit positions per player like lil’ folders :open_file_folder:
• Fire hitting:FireServer(…) first, then Attack:FireServer(…) right after!

⸝

Want me to client code next too?? :pleading_face::point_right::point_left: Or help make the hitbox sparkle or animate or something?? let’s make it cuuute and work!! :computer::cherry_blossom::boom:

i had the hitting . onserver outside the attack.onserver before, but i placed it in thinking it would fix it xd also the way you type is so cuteeeee, imma try doing this to fix

For cooldowns you might want to use task.delay().


rend.RenderStepped:Connect(cooldown)
task.delay(10 , function()
       qcd = false
end)

YESSSS IT WORKS THANKS!!! :heart_eyes: :heart_eyes: :heart_eyes:

1 Like