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

Why my script won't detect the change in the string value?

Asked by 6 years ago

LocalScript

01local UIS = game:GetService("UserInputService")
02local NPC = workspace:WaitForChild("NPC")
03 
04local event = game.ReplicatedStorage.Substract
05local CurrentNPC = game.ReplicatedStorage.CurrentNPC
06 
07local player = game:GetService("Players").LocalPlayer
08local char = player.Character or player.CharacterAdded:wait()
09 
10local maxDistance = 10
11 
12UIS.InputBegan:Connect(function(input)
13    if input.UserInputType == Enum.UserInputType.Keyboard then
14        if input.KeyCode == Enum.KeyCode.E then
15            if UIS:GetFocusedTextBox() == nil then
View all 26 lines...

Server Script

01local event = game.ReplicatedStorage.Substract
02local NPC = workspace.NPC
03local currentNPC = game.ReplicatedStorage.CurrentNPC
04 
05 
06event.OnServerEvent:connect(function(player)
07    local stats = player:WaitForChild("leaderstats")
08    local present = stats:WaitForChild("Presents")
09    if present ~= nil then
10       if present.Value >= 1 then
11        present.Value = present.Value - 1
12        print(currentNPC.Value)
13        end
14    end
15end)

When I print(currentNPC.Value) which is in the ServerScript, it just prints blank. But when I checked the value for the CurrentNPC in the ReplicatedStorage it is the name of the NPC for example "Test". When I print the currentNPC in the LocalScript it does show the name of the NPC.

0
What is the expected output supposed to be? SteamG00B 1633 — 6y
0
Supposed to print "Test" which is the name of the npc. Actually I want to compare if the name of the CurrentNPC is the same with v.Name(in LocalScript) xiFrosty 13 — 6y

1 answer

Log in to vote
0
Answered by
aschepler 135
6 years ago

When a server Script modifies an object in ReplicatedStorage, that change replicates to all clients. But when a client LocalScript modifies an object in ReplicatedStorage, that change does not replicate anywhere outside that client (and the change will probably be discarded the next time the server sends an update for it).

So do any important changes in the server Script, by passing any needed data through your RemoteEvent:

LocalScript:

1...
2if mag <= 10 then
3    event:FireServer(v.Name)
4end
5...

Script:

01...
02event.OnServerEvent:connect(function(player, npcName)
03    local stats = player:WaitForChild("leaderstats")
04    local present = stats:WaitForChild("Presents")
05    if present ~= nil then
06       if present.Value >= 1 then
07        present.Value = present.Value - 1
08        currentNPC.Value = npcName
09        print(currentNPC.Value)
10       end
11    end
12end)
Ad

Answer this question