Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Still struggling! What goes in a local script vs a server script?

Asked by 5 years ago

I'm still struggling with this one. I get the idea of containing everything that applies to just one client within a local script (like GUIs and stuff), and I get the idea of data protection using server scripts to hide access to code from the clients, but I'm still not sure exactly what goes where. For example, here's a script I'm working on right now to create a randomized battle within my game:

 -- The following is on a local script in starterplayer:
 local player = script.Parent.Parent
 local encounterEvent = game.ReplicatedStorage:WaitForChild("EncounterEvent")
 local encounterRate = .8
 math.randomseed(tick())

 while wait(5) do
   if player.Character.Stats.Encounter.Value == true then
     local chance = math.random()
     if chance < encounterRate then
       encounterEvent:FireServer()
     end
   end
 end

-- The following is in the server script:
   local encounterEvent = game.ReplicatedStorage:WaitForChild("EncounterEvent")

   local function startEncounter(player)
     -- Get names
     local zone = player.Character.Stats.Zone.Value
     local stats = player.Character.Stats
     local humanoid = player.Character.Humanoid
     print("Starting Encounter in "..zone)
     -- Turn off encounters for battle
     stats.Encounter.Value = false
     -- Create part at player's position to return after battle
     local returnPoint = game.ServerStorage.ReturnPoint:Clone()
     returnPoint.Parent = player.Character
     returnPoint.Position = player.Character.Head.Position
     -- Warp to correct zone battlefield
     if zone == "Slime Woods" then
       print("Warping to battle")
       player.Character.Head.CFrame = workspace.SlimeWoods.EncounterP1.CFrame
     end
     -- Turn off player movement
     local speed = humanoid.WalkSpeed
     local jump = humanoid.JumpPower
     humanoid.WalkSpeed = 0
     humanoid.JumpPower = 0
     -- Set Camera
     -- Battle control loop
     local battleOver = false
     while not battleOver do
       wait(5)
       battleOver = true
     end
     -- Return player to zone
     player.Character.Head.CFrame = returnPoint.CFrame
     -- Restore player movement
     humanoid.WalkSpeed = speed
     humanoid.JumpPower = jump
     -- Restore camera
     -- Turn encounters back on
     stats.Encounter.Value = true
   end

    encounterEvent.OnServerEvent:Connect(startEncounter)

Now I just can't really look at this and figure out what is supposed to go where. Any help?

0
You understand client and server but don't understand how to script them? Your code needs to meet the client and server respectively. Sapppower 17 — 5y
0
I think I got that, I just don't know what code actually needs to be client side and what code needs to be server side. Out of my code I posted, can you point out what needs to be moved? TerminusEstKuldin 40 — 5y

Answer this question