Need Help refrencing the player

so i have a module script here, just wondering how I will get the player who is hit, because I was to do stuff to the player upon being hit and I’m completely lost.

local module = {}

module.Hitbox = function(player,Cframe,Size, Damage)
	local rs = game:GetService("ReplicatedStorage")
	local chr = player.Character
    local hitboxCFrame = chr.PrimaryPart.CFrame * Cframe
    local hitboxSize = Size
    local damage = Damage
    local hitbox = rs.Hitbox:Clone()
    hitbox.Parent = workspace
    hitbox.CFrame = hitboxCFrame
    hitbox.Size = Vector3.new(hitboxSize, hitboxSize, hitboxSize)
    local hitcontent = workspace:GetPartBoundsInBox(hitboxCFrame, hitboxSize)
    local hitlist = {}

    for _,v in pairs(hitcontent) do
	    if v.Parent:FindFirstChild("Humanoid") and v.Parent ~= chr and not table.find(hitlist, v.Parent) then
		table.insert(hitlist, v.Parent)
		v.Parent.Humanoid:TakeDamage(damage)
	    end
    end

end




return module

server script is

local rs = game:GetService("ReplicatedStorage")

local stunnedevent = rs:WaitForChild("StunnedEvent")

local HitboxModule = require(rs:WaitForChild("Modules").HitboxModule)

stunnedevent.OnServerEvent:Connect(function(player)
	HitboxModule.Hitbox(player,CFrame.new(0,0,-2.5),Vector3.new(5,5,5),10)
	

	
	
end)

1 Like

You need to return your hitlist in your hitbox function. Right now, calling the function only calculates the hitbox. It doesn’t give return which players are hit. To do this, you could simply return the hitlist table at the end of the function.

local module = {}

module.Hitbox = function(player,Cframe,Size, Damage)
	local rs = game:GetService("ReplicatedStorage")
	local chr = player.Character
    local hitboxCFrame = chr.PrimaryPart.CFrame * Cframe
    local hitboxSize = Size
    local damage = Damage
    local hitbox = rs.Hitbox:Clone()
    hitbox.Parent = workspace
    hitbox.CFrame = hitboxCFrame
    hitbox.Size = Vector3.new(hitboxSize, hitboxSize, hitboxSize)
    local hitcontent = workspace:GetPartBoundsInBox(hitboxCFrame, hitboxSize)
    local hitlist = {}

    for _,v in pairs(hitcontent) do
	    if v.Parent:FindFirstChild("Humanoid") and v.Parent ~= chr and not table.find(hitlist, v.Parent) then
		table.insert(hitlist, v.Parent)
		v.Parent.Humanoid:TakeDamage(damage)
	    end
    end
    return hitlist
end




return module

And once you do that, on the server you can access the hitlist when calling the function like so:

local rs = game:GetService("ReplicatedStorage")

local stunnedevent = rs:WaitForChild("StunnedEvent")

local HitboxModule = require(rs:WaitForChild("Modules").HitboxModule)

stunnedevent.OnServerEvent:Connect(function(player)
	local HitPlayers = HitboxModule.Hitbox(player,CFrame.new(0,0,-2.5),Vector3.new(5,5,5),10) -- This will set the variable "HitPlayers" to a table with the characters that your hitbox hit inside of it.
	

	
	
end)

Actually, you don’t return the players who are hit you return their characters.

would be:

table.insert(hitlist, game:GetService("Players"):GetPlayerFromCharacter(v.Parent))

You are right. That is what I meant to say. Usually when using hitboxes they would prefer to have the character instance over the player anyway.

quick question, do yk why im getting this error?

image

local rs = game:GetService("ReplicatedStorage")

local stunnedevent = rs:WaitForChild("StunnedEvent")

local HitboxModule = require(rs:WaitForChild("Modules").HitboxModule)

stunnedevent.OnServerEvent:Connect(function(player)
	local hitplayers = HitboxModule.Hitbox(player,CFrame.new(0,0,-2.5),Vector3.new(5,5,5),10)
	local bloodvfx = game:GetService("ServerStorage").BloodVfx:Clone()
	local chr = player.Character
	local hrp = chr.PrimaryPart
	bloodvfx.Parent = workspace
	bloodvfx.CFrame = CFrame.new(hitplayers.HumanoidRootPart.CFrame)
	task.wait(0.5)
	bloodvfx:Destroy()
	
	


	
	
end)


Make sure your variable bloodvfx is being properly defined. Usually, you store things like vfx in ReplicatedStorage which you reference at the start using rs but try to grab bloodvfx from ServerStorage which has other use cases. Try using ReplicatedStorage instead