Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why does this script work in single player but not in multiplayer?

This script works in single player but not multiplayer. When I use it in multiplayer it says "ClickEvent is not a valid member of tool." I have a RemoteEvent named ClickEvent though.

--- Regular Script ---
local tool = script.Parent
local clickEvent = tool.ClickEvent
local spawnblock = game.Workspace.spawnblock
local clickEventConnection

local function createPart(location)
    local car = game.ReplicatedStorage.Assets.Car
    local newCar = car:Clone()
    newCar.Name = "Car"
    newCar.Parent = game.Workspace
    newCar:MakeJoints()
    newCar:MoveTo(spawnblock.Position)
end

local function onClick(player, clickLocation)
    createPart(clickLocation)
end

local function onEquip()
    clickEventConnection = clickEvent.OnServerEvent:connect(onClick)
end


tool.Equipped:connect(onEquip)

--- LocalScript ---
local tool = script.Parent
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local clickEvent = tool.ClickEvent
 
local function onActivate()
	local clickLocation = game.Workspace.spawnblock
	clickEvent:FireServer(clickLocation)
	tool:Remove()
end
 
tool.Activated:connect(onActivate)

1 answer

You should try using :WaitForChild() to make sure everything has fully loaded.

You can also use the function repeat until to make sure more variables loaded too.

Example with your code

--Example using :WaitForChild()
local clickEvent = tool:WaitForChild('ClickEvent')

--Example using repeat until
repeat wait() until tool.ClickEvent

1 comment

0

This fixes the error but breaks the tool

This discussion is preserved as it was. New answers, comments, and votes are closed.