LocalScript
01 | local UIS = game:GetService( "UserInputService" ) |
02 | local NPC = workspace:WaitForChild( "NPC" ) |
03 |
04 | local event = game.ReplicatedStorage.Substract |
05 | local CurrentNPC = game.ReplicatedStorage.CurrentNPC |
06 |
07 | local player = game:GetService( "Players" ).LocalPlayer |
08 | local char = player.Character or player.CharacterAdded:wait() |
09 |
10 | local maxDistance = 10 |
11 |
12 | UIS.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 |
Server Script
01 | local event = game.ReplicatedStorage.Substract |
02 | local NPC = workspace.NPC |
03 | local currentNPC = game.ReplicatedStorage.CurrentNPC |
04 |
05 |
06 | event.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 |
15 | 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.
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 | ... |
2 | if mag < = 10 then |
3 | event:FireServer(v.Name) |
4 | end |
5 | ... |
Script:
01 | ... |
02 | event.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 |
12 | end ) |