Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
1

Why doesn't this ObjectValue adding script work?

Asked by 2 months ago
Edited 2 months ago

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

0
do not forget to accept ifmy answer helps incapaxx 3107 — 2mo
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 aandmprogameing 38 — 2mo

1 answer

Log in to vote
0
Answered by
incapaxx 3107 Badge of Merit Moderation Voter Community Moderator
2 months ago
Edited 2 months ago

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

0
FYI if you receive PlayerAdded by a script it only runs the code once as the server only sends it once aandmprogameing 38 — 2mo
0
The problem is it still doesn't add the ObjectValue Boi52893642864912 25 — 2mo
Ad

Answer this question