I’ve been trying to get my tool giver script to work for a while now. Long story short, I have a server script that check whether or not -inside of a CharacterAdded() function- the player owns a certain gamepass. If the player does, it should give a tool but it does’nt… I’ve tried print() to see if i could receive any data on where the if statement was, but literally nothing shows up. The weird part is that the script works only if its in a PlayerAdded() function, but not CharacterAdded().
Here’s my code:
players_service.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
if market_service:UserOwnsGamePassAsync(player.UserId, gamepass_ids["PermanentTrowel"]) then
local perm_trowel = game.ReplicatedStorage.Items:WaitForChild("Permanent Trowel"):Clone()
perm_trowel.Parent = player.Backpack
print("perma trowel given to "..player.Name)
else
print("no trowel")
end
if market_service:UserOwnsGamePassAsync(player.UserId, gamepass_ids["PermanentLandMine"]) then
local perm_landmine = game.ReplicatedStorage.Items:WaitForChild("Permanent LandMine"):Clone()
perm_landmine.Parent = player.Backpack
print("perma landmine given to "..player.Name)
else
print("no landmine")
end
player.parameters.InSpawnZone.Value = false
character:WaitForChild("Humanoid").Died:Connect(function()
if workspace:FindFirstChild(player.Name.."'s Ball") then
workspace:FindFirstChild(player.Name.."'s Ball"):Destroy()
end
end)
end)
end)
I am not 100% sure, but I believe the issue is that you have the CharacterAdded command inside the PlayerAdded command, which means it will only run the code if the character AND player is loaded at the same time, which is very unlikely.
I think the reason why CharacterAdded isn’t working/firing is because sometimes, especially when the first player joins the server, the character loads first before PlayerAdded fire.
It’s best to use the function connected to CharacterAdded as a separate script inside StarterPlayer.StarterCharacterScripts instead. In this way, the script will always run when the player’s character loads or resets.
My question is why do we need CharacterAdded in the first place, tools are (typically) stored in the Player’s backpack, which the character isn’t needed for.
Alternatively, if you still need the PlayerAdded event, you could set a parameter called character and set it to player.Character or player.CharacterAdded:Wait(). This will get the player’s character if it already exists and if not, it can just wait for the CharacterAdded to fire then get the player’s character.
Using this method, the script would look like this:
players_service.PlayerAdded:Connect(function()
local character = player.Character or player.CharacterAdded:Wait()
if market_service:UserOwnsGamePassAsync(player.UserId, gamepass_ids["PermanentTrowel"]) then
local perm_trowel = game.ReplicatedStorage.Items:WaitForChild("Permanent Trowel"):Clone()
perm_trowel.Parent = player.Backpack
print("perma trowel given to "..player.Name)
else
print("no trowel")
end
if market_service:UserOwnsGamePassAsync(player.UserId, gamepass_ids["PermanentLandMine"]) then
local perm_landmine = game.ReplicatedStorage.Items:WaitForChild("Permanent LandMine"):Clone()
perm_landmine.Parent = player.Backpack
print("perma landmine given to "..player.Name)
else
print("no landmine")
end
player.parameters.InSpawnZone.Value = false
character:WaitForChild("Humanoid").Died:Connect(function()
if workspace:FindFirstChild(player.Name.."'s Ball") then
workspace:FindFirstChild(player.Name.."'s Ball"):Destroy()
end
end)
end)
You could use the PlayerAdded function which fires when the player first joins the game and then a CharacterAdded function which fires every time the player respawns.
This probably isn’t the best way but it should work.
This is’nt what i’m looking for… I don’t want to seem picky but I don’t need the character itself, I need to check if the player owns the gamepass everytime the character loads so that even when the player resets, it still gives the tool.
Sorry if I don’t fully understand the goal here, I haven’t fully read the script yet. Sorry for that
So let’s say a player owns a gamepass while playing the game. A little while later, the player removes the gamepass from your inventory. You want that after they removed it from their inventory, after they reset, the game doesn’t grant them the tool anymore. Right?
But what i’m trying to achieve is a way for the player to receive the tools everytime their character loads / resets becaue otherwise, players lose their tools after dying. So my idea was to check if the player has the gamepass everytime their character loads so that they can receive their tools even after dying.
I see. When the player joins the game, no need to use the CharacterAdded event. After checking if they own the gamepass, you can directly parent the tool into their backpack and then clone the tool and parent it to their StarterGear. The StarterGear lets the tools inside it to be automatically given to the player everytime they die or reset their character. Like I mentioned earlier.
For the Humanoid.Died event, you can simply use the character variable that I mentioned earlier.