Script errors even though nothing is wrong

  1. What do you want to achieve?

I want to make a tool that when activated, spawns a model in the world.

  1. What is the issue?

When I run my script, it errors for seemingly no reason.

Error: 22:22:14.746 Players.rolego222.Backpack.chair spawner.LocalScript:17: Expected identifier when parsing expression, got ‘local’ - Studio - LocalScript:17

(There is nothing on line 17 so idk how there is something wrong with it)

Here is my code:

local function onPlayerAdded()


	local player = game.Players.LocalPlayer
	local character = player.Character or player.CharacterAdded:Wait()
	local chair = game.ReplicatedStorage.epic_chair
	local IsChairSpawned = false
	local tool = script.Parent
	print "yes"

	local function spawnchair()


		local chairclone = chair:Clone()
		chairclone.Parent = game.Workspace


	end

	game.Players.LocalPlayer.CharacterAdded:Connect(onPlayerAdded())

	tool.Activated:Connect(function()
		if not IsChairSpawned then
			spawnchair()
		end
	end)
end



Is there something wrong with my code or is this a studio bug?

1 Like

Even when I make all of my code a comment, therefore making it not be read, it still says there is an error with line 17.

Have you tried to click on the error message to see which line it errored in?

heya!

Why game.Players.LocalPlayer.CharacterAdded:Connect(onPlayerAdded()) is inside the function calling itself again? That gonna never start cause CharacterAdded its never been triggered if its inside the function.
Maybe u want to take it out, and change :Connect(onPlayerAdded()) for :Connect(onPlayerAdded)

local function onPlayerAdded()

	local player = game.Players.LocalPlayer
	local character = player.Character or player.CharacterAdded:Wait()
	local chair = game.ReplicatedStorage.epic_chair
	local IsChairSpawned = false
	local tool = script.Parent
	print "yes"

	local function spawnchair()
		local chairclone = chair:Clone()
		chairclone.CFrame = character.HumanoidRootPart.CFrame * CFrame.new(0,10,0)
		chairclone.Parent = game.Workspace
	end

	tool.Activated:Connect(function()
		if not IsChairSpawned then
			spawnchair()
		end
	end)
end

-- Outside the function
game.Players.LocalPlayer.CharacterAdded:Connect(onPlayerAdded)

btw. If the tool is not in StarterPack, CharacterAdded will never trigger, if you collect the tool from another place it will never happen

1 Like

My bad, there was another script with the same name that did have an error on line 17.