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

Server Script doesn't want to interact with Player. Script is in workspace. What to do?

ServerScript: (This script is a server script, not a localscript)

local NPC = script.Parent.Parent -- who is talking (in this case workspace.NPC)
local CC = Enum.ChatColor.White -- you can change Red to Blue, Green or White
local waittime = 2 -- time you have to wait for each message


--guts (DONT TOUCH)
local function Chat(message: string, color)
	game:GetService("Chat"):Chat(NPC.Head, message, CC)
end
local function Jump()
	NPC.Humanoid.Jump = true
end
local function Move(pos: Vector3, spawn_thread)
	spawn_thread = spawn_thread or false
	NPC.Humanoid:MoveTo(pos)
	if not spawn_thread then NPC.Humanoid.MoveToFinished:Wait() end
end
local walkpoint = script.Parent.Parent.WalkPoints



--CHAT AND MOVE
	
wait(15)

Chat("A gun! My gun!!!")
Move(walkpoint["1"].Position) -- you can add this to a certain part so the NPC moves in order of the walkpoints (workspace.WalkPoints)
wait(waittime)
	
Chat("bruh")
wait(waittime)
Chat("i cant pick it up :/")
Move(walkpoint["2"].Position) -- make sure you change the number also
wait(waittime)
Chat(";gun")
wait(waittime)
game.Workspace.Handgun.Parent = game.Workspace.NPC
Jump()
Chat("YAY")
wait(2)
Chat("BRUH")
wait(2)
Chat("I GOT STUCK NOOO")
wait(2)
Chat("hmm")
wait(2)
Chat("i should download an exploit, wait 15 secs")
wait(15)
Chat(";fly")
wait(2)
game.Workspace.NPC:BreakJoints()
wait(2)
game.Workspace.NPC:Destroy()
wait(3)
game.Players.LocalPlayer.Character:Destroy()

Error: attempt to index nil with 'Character'

1 comment

0

'LocalPlayer' can only be used in localscript not server scripts. To have Local Script and Server scripts communicate together use RemoteEvents: https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events

1 answer

✓ Accepted answer

The problem is on the last line, LocalPlayer doesn't work on the server, if you want to kill all the players in the server, loop through the players and kill them, also, don't destroy the character, they won't be able to respawn (unless you don't want them to lol)

-- code before lol
-- should be on last line with wait(3) and localPlayer, just replace those lines
wait(3)
for _, player in pairs(game.Players:GetPlayers()) do
	if (player.Character) then
		-- if the player has a character
		
		-- player.Character:Destroy() -- Optional

		local humanoid = player.Character:FindFirstChild("Humanoid")
		if (humanoid) then
			-- If the player has a humanoid
			humanoid.Health = 0
		end	
	end
end

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