charVariable only works upon player spawn

i have a script that looks for the player in the workspace using ‘game.Players.PlayerAdded:Connect’
inside my charvariable, the issue is that only runs when the player joins the game, and i have a seperate script that destroys() and clones() that script once the player unequips the tool, making the charvariable break.

is there any other way to mention the player in the workspace using a script (not local)?

Have you tried :GetPlayerFromCharacter?

if this script you have is under StarterCharacterScripts, then it will run every time the players character spawn

Can you provide more information ?

im wondering if theres any other way to get the players model in the workspace from a script
this is the current part in my code that sorta does this:

local charVariable
game.Players.PlayerAdded:Connect(function(player)
   player.CharacterAdded:Wait()
   charVariable = player.Character
end)

the issue with this is that is only runs when the player joins the game. so it breaks if the player resets or if i clone() the script

based off of what i looked at on the creator documentation that is only for finding the player in game.Players
im trying to find the player’s model in the workspace.

Use player.CharacterAdded. This will run when the character gets added to the workspace compared to when the player joins

CharacterAdded

does that work for regular scripts or localscripts only?
if it does work for regular scripts i’ll try it out

Can be used in a local and server script.

local charVariable
game.Players.PlayerAdded:Connect(function(player)
   player.CharacterAdded:Connect(function(Character)
   charVariable = Character
end)

In that case you can use this

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		char.Humanoid.Died:Connect(function()
			-- your code
		end)
	end)
end)

correct me if im wrong but im supposed to put the entirety of my script in here? im not trying to make the entire code play when someone resets or joins. here’s more context for what im looking for:

the model im using this code for is a 2010s cellphone model im trying to fix, im specifically working on the GUI code. the GUI has a keypad and 5 message slots next to it that will display the Tool.Words.Value in the following code.

   				if (b.UserId == tonumber(number)) and charVariable:FindFirstChild("CellPhone") then
   					Tool.Words.Value = "Player busy" else

this part in the code specifically checks for when you type the userID (their phone number) into the keypad and if they already have the cellphone equipped (if they do, the cellphone model will be in their player model in the workspace) it displays the text “Player busy” in one of the message slots.

charVariable is supposed to be the players model in the workspace, and im trying to find a way to find their model consistently.

GUI:
image

i was able to fix this by changing charVariable to search the workspace for the players name that is being called. then by doing that i FindFirstChild the charVariable to see if they have the cellphone in their inventory, if not, the call will continue, if they do, it will display the “Player Busy” message

local charVariable = workspace:FindFirstChild(b.Name) -- Searches workspace for player being called (b)
print("Player being called is" ..tostring(charVariable)) -- Displays player being called with name
if (b.UserId == tonumber(number)) and charVariable:FindFirstChild("CellPhone") then -- Searches if player has CellPhone in their model
Tool.Words.Value = "Player busy" -- If they do it displays "Player busy" in messages
elseif (b.userId == tonumber(number)) and (b.Character:FindFirstChild("Humanoid") ~= nil) then -- Calls normally if FindFirstChild did not find the CellPhone
Tool.Calling.Value = b.Backpack.CellPhone
Tool.Words.Value = "Calling " ..b.Name.. "..." -- Calls player

thank you to everyone that tried helping

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.