Scripts inside tool no longer work after player resets

I have a tool and a script that clones the tool from serverstorage to the player backpack and startergear when you purchase it so that way it doesnt go away after a player resets but the scripts work at first but then after resetting they no longer work.
Server script:

game.ReplicatedStorage.PurchaseBucket.OnServerEvent:Connect(function(player,amount)
	player.leaderstats.Shells.Value= player.leaderstats.Shells.Value - 50
	game.ServerStorage.Bucket:Clone().Parent = player.Backpack
	game.ServerStorage.Bucket:Clone().Parent = player.StarterGear
end)

Purchase script inside GUI button:

local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
	if player.leaderstats.Shells.Value >= 50 then
		-- Checks if player has enough shells
		game.Workspace.PurchaseSuccessful:Play()
		game.ReplicatedStorage.PurchaseBucket:FireServer(0)
		-- Fires RemoteEvent that charges player
		script.Parent.Text = ("Purchased")
		script.Disabled = true
	else
		game.Workspace.PurchaseFailed:Play()
		script.Parent.Text = ("Insufficient funds!")
		wait(3)
		script.Parent.Text = ("Purchase for 50 Shells...")
	end
end)

Script inside tool:

local player = script:FindFirstAncestorWhichIsA("Player") or game:GetService("Players"):GetPlayerFromCharacter(script.Parent.Parent)
local character = player.Character or player.CharacterAdded:Wait()
local Animator = character:WaitForChild("Humanoid"):WaitForChild("Animator")

local tool = script.Parent 

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://7378586179"

local loaded = Animator:LoadAnimation(animation)
local GUI = player.PlayerGui

tool.Activated:Connect(function()
	loaded:Play()
	GUI.BucketGui.Enabled = true
	tool.Sand.Transparency = 0
end)

local GUI = player.PlayerGui

script.Parent.Unequipped:Connect(function()
	GUI.BucketGui.Enabled = false
end)

game.ReplicatedStorage.EmptyBucket.OnServerEvent:Connect(function()
	script.Parent.Sand.Transparency = 1
end)
1 Like

Which script in particular stops working? Can you be more specific?

It’s probably a case of losing the character model I’m guessing. Try retrieving the character model when the tool is equipped.

local player = script:FindFirstAncestorWhichIsA("Player") or game:GetService("Players"):GetPlayerFromCharacter(script.Parent.Parent)
local GUI = player.PlayerGui
local tool = script.Parent 

function GetAnimation(Id)
    local character = player.Character or player.CharacterAdded:Wait()
    local Animator = character:WaitForChild("Humanoid"):WaitForChild("Animator")

    local animation = Instance.new("Animation")
    animation.AnimationId = Id

    return Animator:LoadAnimation(animation)
end

tool.Activated:Connect(function()
    local loaded = GetAnimation("rbxassetid://7378586179")
    loaded:Play()

    GUI.BucketGui.Enabled = true
    tool.Sand.Transparency = 0
end)

script.Parent.Unequipped:Connect(function()
    GUI.BucketGui.Enabled = false
end)

game.ReplicatedStorage.EmptyBucket.OnServerEvent:Connect(function()
    script.Parent.Sand.Transparency = 1
end)

this is for that script, I took the approach of using a function

basically when you reset the character is nil, because that model is not inside workspace anymore

see if it works

1 Like

Oh sorry the script inside the tool that one script that I linked here stops working

It stops working after the player resets right? Does the output show any error messages?

it is because of the character variable, that’s why it breaks

Yep, I was just making sure. To fix, instead of using the character variable just use player.Character instead.

I would start a new topic, seems like a better option

1 Like