Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why doesn't this ObjectValue adding script work?

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

2 comments

0

do not forget to accept ifmy answer helps

0

Your problem is PlayerAdded as this function is called when a player joins the server and can not be called by a script only received

1 answer

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.

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);

2 comments

0

FYI if you receive PlayerAdded by a script it only runs the code once as the server only sends it once

0

The problem is it still doesn't add the ObjectValue

This discussion is preserved as it was. New answers, comments, and votes are closed.