Script stops working after resetting the character

This script is just a pickup script, I recognized that it only works 1 time and changed it into this. But this time, it never binds the action. I thought maybe character is being created faster than the script and put the event on top but nothing changed. If that’s the case, it would work on second anyways.

(It’s in StarterPlayerScripts)

-- Services
local collection = game:GetService("CollectionService")
local contextaction = game:GetService("ContextActionService")
-- Variables
local plr = game.Players.LocalPlayer
local char = plr.CharacterAdded:Wait()
local head = char:WaitForChild("Head")
local taggedParts = collection:GetTagged("ItemGiver")
local remotes = game.ReplicatedStorage.RemoteEvents
local remote = remotes.PickupItem
-- Functions
local function actionHandler(actionName, inputState, inputObject)
	if actionName == "Pickup" then
		if inputState == Enum.UserInputState.Begin then
			for _, v in pairs(taggedParts) do
				if (v.Position - head.Position).Magnitude < 5 then
					remote:FireServer(v.Name)
				end
			end
		end
	end
end

local function bindAction()
	contextaction:BindAction("Pickup", actionHandler, true, Enum.KeyCode.F)
	
	local button = contextaction:GetButton("Pickup")
	if button then
		contextaction:SetPosition("Pickup", UDim2.new(0.6,0,0.1,0))
		contextaction:SetTitle("Pickup", "Pick")
	end
end

local function unbindAction()
	contextaction:UnbindAction("Pickup")
end

plr.CharacterAdded:Connect(function()
	bindAction()
end)

plr.CharacterRemoving:Connect(function()
	unbindAction()
end)

Still up. (30 charsssssssssss)

In my understanding, I think that instead of doing that, you could do CharaterAdded:Connect() and putting char = plr.Character in that connection so it would fire every time the player resets(it also includes the first time the player joins and spawns in the game), doing local char = plr.CharacterAdded:Wait() just does that once upon the first spawn of the player.
May be wrong though as I am still learning along the way.

Forgot to mention that when a character resets, it gets destroyed and the your variables with char are still referring to the old character so that’s the reason why your script stops after you reset.
Again I am not fully sure but that’s my thought.

When the character re-spawns that reference to character will become nil since basically a new character model will be created, thus pointing to a different location in memory. What you need to do is adjust your ActionHandler function. Also, I’m not sure if local scripts may run before the character initially spawns so it would be safer if you manually call bindAction() the first time.

-- Services
local collection = game:GetService("CollectionService")
local contextaction = game:GetService("ContextActionService")
-- Variables
local plr = game.Players.LocalPlayer
local remotes = game.ReplicatedStorage.RemoteEvents
local remote = remotes.PickupItem
--constants
local PICKUP_BINDING_KEY = "Pickup"--better to use a constant when binding to CAS
-- Functions
local function actionHandler(actionName, inputState, inputObject)
    local taggedParts = collection:GetTagged("ItemGiver") --it would be better if you get them inside here since things may be added
    local head = plr.Character and plr.Character:FindFirstChild("Head")
    
	if actionName == PICKUP_BINDING_KEY then
		if inputState == Enum.UserInputState.Begin then
			for _, v in pairs(taggedParts) do
				if (v.Position - head.Position).Magnitude < 5 then
					remote:FireServer(v.Name)
				end
			end
		end
	end
end

local function bindAction()
	contextaction:BindAction(PICKUP_BINDING_KEY, actionHandler, true, Enum.KeyCode.F)
	
	local button = contextaction:GetButton(PICKUP_BINDING_KEY)
	if button then
		contextaction:SetPosition(PICKUP_BINDING_KEY, UDim2.new(0.6,0,0.1,0))
		contextaction:SetTitle(PICKUP_BINDING_KEY, "Pick")
	end
end

local function unbindAction()
	contextaction:UnbindAction(PICKUP_BINDING_KEY)
end

if (plr.Character) then
   --checking if the character already exists
   bindAction() 
end

plr.CharacterAdded:Connect(function()
	bindAction()
end)

plr.CharacterRemoving:Connect(function()
	unbindAction()
end)
2 Likes

Yeah, I knew that it’s something with character variable, I’ll try it some hours later and update this topic.