Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why doesn't this ObjectValue adding script work?

Asked by 5 years ago
Edited 5 years 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 User#24403 69 — 5y
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 52 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years 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 52 — 5y
0
The problem is it still doesn't add the ObjectValue Boi52893642864912 69 — 5y
Ad

Answer this question