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
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.
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
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?
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
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)
No problem. Also, the reason it didn’t work the first time you tried, was because you spelt it as :FirstFirstChildOfClass instead :FindFirstChildOfClass