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

Why cant my server script define player?

So basically I have two scripts one is server side and the other one is local. One is in serverscriptservice. Other one is in StarterGUI.

So I am wondering why when I press the button to spawn blocks it decides to go ahead and say that localplayer is not a part of player.

---Server Code local Players = game:GetService("Players")

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local createPartEvent = ReplicatedStorage:FindFirstChild("Spells")

local cooldown = "on"

local function Recieved(player)

if cooldown ~= "off" then

local torso = Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")

local Part = Instance.new("Part")

Part.Parent = torso

Part.CFrame = torso.CFrame + torso.CFrame.LookVector * 10

cooldown = "off"

wait(10)

cooldown = "on"

wait(10)

Part:Destroy()

end

end

createPartEvent.OnServerEvent:Connect(Recieved)

--local script
local Players = game:GetService("Players")

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Spells = ReplicatedStorage:WaitForChild("Spells")

local UIS = game:GetService("UserInputService")

local cooldown = "on"

UIS.InputBegan:Connect(function(input)

if input.UserInputType == Enum.UserInputType.Keyboard then -- Checks if the input was related to a keyboard

if input.KeyCode == Enum.KeyCode.Q then

if Players.LocalPlayer.Character then

Spells:FireServer()

end

end

end

end)

2 comments

0

Sorry for the script being hard to copy

0

LocalPlayer can't be replicated over the server/client boundary. It's strictly client-only.

1 answer

✓ Accepted answer

You cannot use LocalPlayer in a server-sided script. However, you can use the player parameter that is passed by the createPartEvent you connected.

--Server Code  
local Players = game:GetService("Players")

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local createPartEvent = ReplicatedStorage:FindFirstChild("Spells")

local cooldown = "on"

local function Recieved(player)

	if cooldown ~= "off" then

		local torso = player.Character:WaitForChild("HumanoidRootPart")

		local Part = Instance.new("Part")

		Part.Parent = torso

		Part.CFrame = torso.CFrame + torso.CFrame.LookVector * 10

		cooldown = "off"

		wait(10)

		cooldown = "on"

		wait(10)

		Part:Destroy()

	end

end

createPartEvent.OnServerEvent:Connect(Recieved)

If you have any questions, feel free to leave a comment. I hope this will work.

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