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

Server Script not changing values inside local player?

Asked by 4 years ago
Edited 4 years ago

Hello. So, I have this server script which is supossed to change some values inside the local player when it receives a remoteEvent, but for some reason it does not change anything. Can anyone help me out?

Here's the script: (oh, sorry if it's a little bit messy, I am new to making my own projects without tutorials.)

game.ReplicatedStorage.TaskFinished.OnServerEvent:Connect(function(player)
    local ongoingtasks = player.values.ongoingtasks.Value
    local finishedtasks = player.values.finishedtasks.Value
    ongoingtasks = false -- THis does not work.
    finishedtasks = finishedtasks + 1 --This does not work.
    player.PlayerGui.Tasks.objective.Text = "Objective: None"
    player.PlayerGui.Tasks.main.current_task.Text = "Current task: None"
    print("Finished task correctly.")
end)

3 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Problems

Because of how DataTypes works, When you index a Primitive Datatype:

  • Number
  • Boolean
  • Strings

Through a Table or Userdata you get a Copy of it. In your example You saved the value of a ValueObject in to a variable which is a Number, thus You get a Copy of it.

When you access a Table or Userdata you get a refference to a memory address. When you try to print a Table or Userdata you will get something like this type: XXXXXXXX Where type is the Datatyp, Table or Userdata and the X's are a Hexadecimal value to a memory address. So changing it will change it on That memory address. WHen you index Table or Userdata trough another Table or Userdata you get a Table or Userdata which has the SAME Adress, Thus changing it will change the Same table.

Solutions

Instead of saving the Value save to actual ValueObject each time you need it, Index the Value property

Example

local ongoingtasks = player.values.ongoingtasks -- Define an Variable to point to a Instance which is a UserData

ongoingtasks.Value = false -- Change the value Property of ValueObject to false

NOTE Changing something in The PlayerGui is advised to be done in a LocalScript

0
Wow, thank you for the help! Javgarag 2 — 4y
0
Please accept the answer :D Luka_Gaming07 534 — 4y
0
There ya go :D Javgarag 2 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
local ongoingtasks = player.values.ongoingtasks.Value

you are storing the value inside the ongoingtasks BoolValue object's .Value property inside the variable ongoingtasks. Changing the value inside ongoingtasks does not change the value inside the actual BoolValue inside the player. To do so, you have to set its .Value property like so:

player.values.ongoingtasks.Value = false

To make the code more readable, I would store the player.values.ongoingtasks BoolValue object inside a variable:

local ongoingtasks = player.values.ongoingtasks
ongoingtasks.Value = false

The same concept can be applied to finishedtasks.

local finishedtasks = player.values.finishedtasks
finishedtasks.Value = finishedtasks.Value + 1

Here's what your code should look like:

game.ReplicatedStorage.TaskFinished.OnServerEvent:Connect(function(player)
    local ongoingtasks = player.values.ongoingtasks
    local finishedtasks = player.values.finishedtasks
    ongoingtasks.Value = false -- THis does not work.
    finishedtasks.Value = finishedtasks.Value + 1 --This does not work.
    player.PlayerGui.Tasks.objective.Text = "Objective: None"
    player.PlayerGui.Tasks.main.current_task.Text = "Current task: None"
    print("Finished task correctly.")
end)

Unrelated to your question but I see that you're modifying the player's gui through a server script. I recommend you use a local script when it comes to modifying anything related to the player.PlayerGui as a general rule of thumb, since guis by nature are local.

Log in to vote
0
Answered by
Nckripted 580 Moderation Voter
4 years ago

For the record, you can't change those values inside a local script. It will only show up for the player, and other scripts won't see it.

Answer this question