What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I searched Youtube & the Dev Forum for help but couldn’t find what I needed.
Here is my code. Just for testing purposes I added a print statement after
-- This is an example Lua code block
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(Chr)
for a,obj in pairs(Chr:GetChildren()) do
if obj.Name == "Hitbox" then
obj.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
print(obj.Parent.Name.." was touched by "..player.Name)
end
end)
end
end
end)
end)
The reason the hitbox isn’t registering is that you’re running a for loop looking for it right when the character is created, which does not necessarily mean that the hitbox has even been created yet when the script is looking for it. In general, the for loop is rather inefficient here; you can easily wait for the hitbox you’re looking for to exist by using :WaitForChild() (since I assume there is only one hitbox judging by the image).
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(Chr)
local obj = Chr:WaitForChild("Hitbox")
obj.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
print(obj.Parent.Name.." was touched by "..player.Name)
end
end)
end)
end)
--//Services
local Players = game:GetService("Players")
--//Functions
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local Hitbox = character:WaitForChild("Hitbox")
Hitbox.Touched:Connect(function(hit)
local Player = Players:GetPlayerFromCharacter(hit.Parent)
if Player then
print(Hitbox.Name, "was touched by", Player.Name)
end
end)
end)
end)
Have you considered you’re possibly not touching another Player and rather a random part or an NPC (won’t get registered by your :GetPlayerFromCharacter() call.)? If so, then that would explain why it’s not printing out anything. Try simply doing “print(hit)” and I’m willing to bet you’ll find out it works just fine.