Referencing the character from a server script

Hey guys, i’m struggling to reference the character from a Server Script. I can’t implement documentation i found because they use an OnPlayerAdded function. Here’s my code:

ProximtyPrompt = script.Parent

ProximtyPrompt.Triggered:Connect(function(player)
	if player.Character:FindFirstChild("Tool") then
		local gun = player.Character:FindFirstChild("Tool")
		wait(3)
		gun.Bullets.Value = gun.TotalBullets.Value
	end
end)

My goal with this is to find a tool within the player’s character (its a gun), because if its equipped it will go there. I haven’t referenced the character or player properly and i can’t figure out why. Can anyone please point me into the right direction or help me fix this. If the finding the tool is wrong also, please let me know. Thanks in advance

1 Like

Ok. Since you know that the Character is the Tool’s parent, you can use GetPlayerFromCharacter to get the Player object associated with that character.

Hope that helps!

1 Like

I disagree with @RogueMage - GetPlayerFromCharacter would be useful if you had the Tool already, and wanted the Player.

You already have the Player.

You’ll need to give more info about your error, do you see an error message when you trigger the proximity prompt?

There is no error message, just nothing

Are you sure there’s even a Tool inside the player’s character at the time of the event?

There is a tool inside the player’s character. I have it equipped when i press the proximity prompt and in the workspace in the character it shows its there

I think that with some tweaking i might just be able to adapt this with my limited knowledge, thanks for your response

Oh, you’re right. I completely misread the question. My bad.

If there really is a tool in the player’s character, I don’t really know what else could be the problem. Try player.CharacterAdded:Wait() maybe? Or maybe fire a RemoteFunction to the client to pick up the character and send return it?

Put a print(gun) statement in the if and see if it found it.

Is it named “Tool”? Is it in their backpack instead of in their actual character?

OHHH, okay. Is the Tool(which is supposedly a gun) you’re checking for NAMED Tool or is it Gun? If it’s named Tool, I don’t know what to tell you. But if it’s named Gun, use :FindFirstChildOfClass instead of :FindFirstChild. Hope this helps :slight_smile:

1 Like

You can use the CharacterAdded event

game.Players.PlayerAdded:connect(function(Player)
    Player.CharacterAdded:Connect(function(Character)
             ProximtyPrompt = script.Parent

             ProximtyPrompt.Triggered:Connect(function(player)
	            if Character:FindFirstChild("Tool") then
		          local gun = player.Character:FindFirstChild("Tool")
		          wait(3)
		          gun.Bullets.Value = gun.TotalBullets.Value
	            end
           end)
      end)
end)

This should work

Thanks for your response i tried changing it to OfClass, and now it spits an error
Code:

if player.Character:FirstFirstChildOfClass("Tool") then

error: FirstFirstChildOfClass is not a valid member of Model “Workspace.kingdomkind”

This doesn’t seem to work but i suspect for some reason the intValue of bullets isn’t updating for the script, i’ll try to look into getting a remoteevent working for it

Thank you for your assistance! I’ve managed to do it with this code

game.Players.PlayerAdded:connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		ProximtyPrompt.Triggered:Connect(function(player)
			print("Triggered")
			if Character:FindFirstChildOfClass("Tool") then
				local gun = player.Character:FindFirstChildOfClass("Tool")
				wait(3)
				local MaxBullets = gun.TotalBullets.Value
				print(MaxBullets)
				--gun.Bullets.Value = MaxBullets
				AppendChanges:FireClient(Player)
			end
		end)
	end)
end)

Thank you for your help, i’ve managed to do it with this

game.Players.PlayerAdded:connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		ProximtyPrompt.Triggered:Connect(function(player)
			print("Triggered")
			if Character:FindFirstChildOfClass("Tool") then
				local gun = player.Character:FindFirstChildOfClass("Tool")
				wait(3)
				local MaxBullets = gun.TotalBullets.Value
				print(MaxBullets)
				--gun.Bullets.Value = MaxBullets
				AppendChanges:FireClient(Player)
			end
		end)
	end)
end)

Your tip of OfClass helped me alot!

1 Like

No problem. Also, the reason it didn’t work the first time you tried, was because you spelt it as :FirstFirstChildOfClass instead :FindFirstChildOfClass

1 Like