I made weapon with a "blade" and a script that adds a "creator" ObjectValue to whatever Humanoid the blade hits, the only problem is, it won't add anything to the Humanoid (Player or NPC I've tried both) and when I try to detect the Humanoid, it works.
script.Parent.Blade.Touched:Connect(function(hit) game.Players.PlayerAdded:Connect(function(p) p.CharacterAdded:Connect(function(char) local thing = Instance.new("ObjectValue",hit.Parent.Humanoid) thing.Name = "creator" thing.Value = char.Name print(thing.Value and "test") end) end) end)
It won't print anything I've also done this script:
script.Parent.Blade.Touched:Connect(function(hit) game.Players.PlayerAdded:Connect(function(p) local thing = Instance.new("ObjectValue",hit.Parent.Humanoid) thing.Name = "creator" thing.Value =p.Name print(thing.Value and "test") end) end)
Both scripts don't work
Basically the issue is that you are establishing a listener for the Players.PlayerAdded
event. This event is fired when a player joins the game. So someone else who joins your game will receive this ObjectValue
. They might receive multiple, in fact, since due to how the engine handles collisions, multiple will be registered and thus multiple listeners will be established.
All you have to do is copy the code and just get rid of the listeners, and paste the code into your Touched
listener
You also need to check if it was a legitimate user. Because not everything has a humanoid child. And the Value
is an instance not a string.
```lua
script.Parent.Blade.Touched:Connect(function(part)
local humanoid = part.Parent:FindFirstChild("Humanoid"); --// this returns the humanoid if any. nil
if no obj found.
if (humanoid) then --// to make sure there is humanoid --// insert your code here end
end); ```