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 5 years ago

LocalScript

local UIS = game:GetService("UserInputService")
local NPC = workspace:WaitForChild("NPC")

local event = game.ReplicatedStorage.Substract
local CurrentNPC = game.ReplicatedStorage.CurrentNPC

local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:wait()

local maxDistance = 10

UIS.InputBegan:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        if input.KeyCode == Enum.KeyCode.E then
            if UIS:GetFocusedTextBox() == nil then
                for i, v in pairs(NPC:GetChildren()) do
                    local mag = (v.HumanoidRootPart.Position - char.HumanoidRootPart.Position).magnitude
                    if mag <= 10 then
                        CurrentNPC.Value = v.Name
                        event:FireServer()
                    end
                end
            end
        end
    end
end)

Server Script

local event = game.ReplicatedStorage.Substract
local NPC = workspace.NPC
local currentNPC = game.ReplicatedStorage.CurrentNPC


event.OnServerEvent:connect(function(player)
    local stats = player:WaitForChild("leaderstats")
    local present = stats:WaitForChild("Presents")
    if present ~= nil then 
       if present.Value >= 1 then
        present.Value = present.Value - 1
        print(currentNPC.Value)
        end
    end
end)

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 — 5y
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 — 5y

1 answer

Log in to vote
0
Answered by
aschepler 135
5 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:

    ...
    if mag <= 10 then
        event:FireServer(v.Name)
    end
    ...

Script:

...
event.OnServerEvent:connect(function(player, npcName)
    local stats = player:WaitForChild("leaderstats")
    local present = stats:WaitForChild("Presents")
    if present ~= nil then 
       if present.Value >= 1 then
        present.Value = present.Value - 1
        currentNPC.Value = npcName
        print(currentNPC.Value)
       end
    end
end)
Ad

Answer this question