Hello, in my game I made an ocean, which is one big flat brick. I want the player to be able to float in this ocean instead of falling through. So I made a script that does just that (located in the ocean brick in the workspace, normal script):
local swim = false --Debounce local o = script.Parent --Ocean part function onTouched(part) local h = part.Parent:findFirstChild("Torso") if h ~= nil then swim = true local s = game.Players.LocalPlayer.Swim --Variable that is created when the game starts (more on that later) local b = Instance.new("BodyPosition") b.Parent = h b.Position = Vector3.new(h.Position.X,o.Position.Y,h.Position.Z) while true do s.Value = true wait() if h.Position.Y > o.Position.Y then b:Destroy() s.Value = false swim = false end end end end script.Parent.Touched:connect(onTouched)
This script works perfectly fine, and if you're wondering about the "game.Players.LocalPlayer.Swim" its a bool value that another script creates at the beginning of the game so when the player touches the ocean it turns on, so the game knows if the player is in or out of the ocean.
Here is the script that creates the bool value (located in starter player scripts, local script):
game.Workspace:WaitForChild(game.Players.LocalPlayer.Name) s = Instance.new("BoolValue", game.Players.LocalPlayer) s.Name = "Swim" s.Value = false
and here is the script that once the player presses space, it checks to see if that bool value is true or false (also located in the starter player scripts, local script):
game:GetService("UserInputService").InputBegan:connect(function(inputObject, gameProcessedEvent) if inputObject.KeyCode == Enum.KeyCode.Space then local s = game.Players.LocalPlayer.Swim.Value if s == true then print("Swimming") end if s == false then print("Not Swimming") end end end)
These work fine and do what I want, now that I'm done explaining, here is my problem:
It doesn't work in server mode.
When I test the game it works just fine but when I fire up a server it completely fails and I get the error: "Workspace.Ocean.Script:8: attempt to index field 'LocalPlayer' (a nil value)"
Any help? Thanks!
Hello! Normal(Server) scripts can't use game.Players.LocalPlayer, Only local scripts can use game.Players.LocalPlayer.
you can change it to
local s = game.Players:GetPlayerFromCharacter(part.Parent).FindFirstChild("Swim")
and in the second script you should make a server script in ServerScriptStorage
And in the second script that creates that value, you should be using PlayerAdded Event Because that value will not be client side and server script could reach it. Because everything made by local scripts is client side.