I was creating some guns for use, and I also created a shop to go with it. Once the player purchased the gun, everything worked fine and all, it replicated and was put into the player's backpack.

But the thing is, the tool script broke. You couldn't fire, nor reload or do anything with it, it just broke and became a tool with no script. (No, the script didn't delete.)

I'm using a LocalScript inside of a Tool.

Here's my LocalScript:

game.Players.LocalPlayer.CharacterAdded:Connect(function(character)
	local MaxAmmo = 40
	local Ammo = MaxAmmo
	
	local function createBullet()
		local bulletPart = workspace.bullet:Clone()
		bulletPart.Parent = workspace
		bulletPart.Position = script.Parent.Handle.Part.Position
		bulletPart.Orientation = bulletPart.Orientation + (script.Parent.Handle.Orientation - Vector3.new(0,90,0))
		for i = 0,25,10 do
			bulletPart.CFrame = bulletPart.CFrame * CFrame.new(0,0,i)
			wait()
		end
		bulletPart:remove()
	end
	
	local function Damage()
		game.Players.LocalPlayer:GetMouse().Target.Parent:WaitForChild('EnemyHum'):TakeDamage(15)
	end
	
	local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
	while not character do
		character.AncestryChanged:Wait()
	end
	local hum = character:WaitForChild('Humanoid')
	local reloadAnim = hum:LoadAnimation(script.Parent.Animation)
	local fireAnim = hum:LoadAnimation(script.Parent.fire)
	
	
	script.Parent:WaitForChild('MaxAmmo').Value = MaxAmmo
	script.Parent:WaitForChild('Ammo').Value = Ammo
	
	local canDamage = true
	
	local Reloading = false
	local uis = game:GetService("UserInputService")
	script.Parent.Equipped:Connect(function()
		uis.MouseIconEnabled = true
		game.Players.LocalPlayer:GetMouse().Icon = 'rbxassetid://68308747'
	end)
	script.Parent.Unequipped:Connect(function()
		uis.MouseIconEnabled = false
		game.Players.LocalPlayer:GetMouse().Icon = 'rbxassetid://442141180'
	end)
	
	local isFiring = false
	local canFire = true
	local isDamaging = false
	
	local bulletsShot = 0
	
	script.Parent.Activated:Connect(function()
		canFire = true
		if Ammo > 0 and not Reloading then
			while canFire == true do
				isDamaging = true
				script.Parent.Handle.fire:Play()
				fireAnim:Play()
				Ammo = Ammo - 1
				script.Parent.Handle.BillboardGui.Frame.TextLabel.Text = Ammo.." / "..MaxAmmo
				script.Parent.Handle.Part.FrontFlash.Transparency = 0
				script.Parent.Handle.Part.BackFlash.Transparency = 0
				wait()
				script.Parent.Handle.Part.FrontFlash.Transparency = 1
				script.Parent.Handle.Part.BackFlash.Transparency = 1
				createBullet()
				if Ammo <= 0 then
					canFire = false
				end
			end
			while true do
				if isDamaging == true then
					game.Players.LocalPlayer:GetMouse().Target.Parent:WaitForChild('EnemyHum'):TakeDamage(6)
				end
				wait(.1)
			end
		elseif Reloading == false then
			uis.InputBegan:Connect(function(keyCode)
				if keyCode.keyCode == Enum.KeyCode.R then
					if Reloading == false then
						if Ammo <= 0 then
							reloadAnim:Play()
							Reloading = true
							script.Parent.Handle.reload:Play()
							wait(script.Parent.Handle.reload.TimeLength)
							script.Parent.Handle.BillboardGui.Frame.TextLabel.Text = "40 / 40"
							Ammo = MaxAmmo
							Reloading = false
						end
					end
				end
			end)
		end
	end)
	script.Parent.Unequipped:Connect(function()
		isDamaging = false
		canDamage = false
		canFire = false
		Reloading = false
		script.Parent.Handle.reload:Stop()
		reloadAnim:Stop()
	end)
	script.Parent.Deactivated:Connect(function()
		isDamaging = false
		canFire = false
	end)
end)

I'm very sorry if this is hard to read or just too long, but I really need an answer.

Help please!

2 comments

1

I believe your issue lies on line 1. You're waiting for the CharacterAdded event to fire before the rest of the script can execute, but the player's character most likely already exists in the Workspace before you purchase a weapon. Instead you could try using an event that fires when the tool is added to the players backpack such as ChildAdded and get the player's character that way.

0

Yes! It works! Thank you SOOOO much!