I want to make a tool that when activated, spawns a model in the world.
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?
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