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

How do I read a variable from a LocalScript in a Server Script?

Asked by 4 years ago
Edited 4 years ago

I'm trying to access a variable from a localscript inside of a tool in a script in ServerScriptService, and I don't understand how to do it. This is my latest method of doing it:

LocalScript:

01local gun = script.parent
02local player = game.Players.LocalPlayer
03local clipSize = 15
04local maxClipSize = 15
05local reloadTime = 2
06local damage = 10
07--Keys
08local UserInputService = game:GetService("UserInputService")
09local rKey = Enum.KeyCode.R
10-- Obeject Sound Varibles
11local gun_sound = gun.Handle['Gun Shot']
12local empty_sound = gun.Handle.clip_empty
13local reload_sound = gun.Handle.Reload
14 
15gun.Equipped:Connect(function(mouse)
View all 46 lines...

ServerScript:

01game.ReplicatedStorage.RayCastEvent.OnServerEvent:Connect(function(Player, FromP, ToP)
02    local RayCast = Ray.new(FromP, (ToP-FromP).unit * 100)
03    local Part, Position = game.Workspace:FindPartOnRay(RayCast, Player.Character, false, true)
04    --Applying the variable
05    if Part then
06        local Hum = Part.Parent:FindFirstChild("Humanoid")
07        if Hum then
08            print(_G.damageVariable)
09 
10            Hum:TakeDamage(_G.damageVariable)
11        end
12    end
13 
14    local bulletBeam = Instance.new("Part")
15    bulletBeam.Parent = game.Workspace
View all 26 lines...
0
You don't. Ziffixture 6913 — 4y
0
There's no way at all I could do it? Hm..that stinks... Real_BlobKing21 0 — 4y

3 answers

Log in to vote
0
Answered by 4 years ago

Just make a new variable in the server, and get the information from the server.

0
I could do that, but that's not how I want to do it since multiple local scripts are going to be using that server script. I want to keep everything as a variable that I could get from each local script depending on which one is being used at a certain time. If that makes sense. Real_BlobKing21 0 — 4y
0
It sorta(?) does. The easiest(and most efficient) way is to just make a new variable. It's easy, efficient, and most common. I guess you could TECHNICALLY store the variable in a StringValue, or whatever type of Value. Or use RmoteEvents, but just making a new variable is the easiest way knightking_laval -4 — 4y
0
Yea, I decided to make a separate ModuleScript in order to keep all the variables in there and make it neater. If there's any drawbacks too that, then let me know! Real_BlobKing21 0 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

Make a remote event, pass the variable over to the server by doing the following

1local Variable = script.Parent.Parent.TextLabel -- use text for instance, we don't wanna pass .Text for the text label because it has to update every time so we do it in the :FireServer()
2 
3game.ReplicatedStorage.RemoteEvent:FireServer(player, Variable.Text)

local ^

1game.ReplicatedStorage.OnServerEvent:Connect(function(player, Variable)
2Variable = "Hello! How are you today"
3wait(3)
4Variable = ""
5end)

What this does is it will auto pass variable and we don't need to do Variable.Text because it's simply passed through the FireServer, Variable.Text so we just need Variable = "Text Here!"

0
I heard about this but have also heard that using a lot of remote events could lead to heavy exploiting? I don't know if that's true or not, but thank you! Real_BlobKing21 0 — 4y
0
Well in this case, it wont. You can do security and or just regular checks and if it's correct then you can pass it. This wont be hacked because you're passing a certain variable and checking it on the server. Nicholas1088 169 — 4y
Log in to vote
0
Answered by 4 years ago

I created a separate ModuleScript called "Damages" containing:

1local DamageModule = {}
2    DamageModule.handgunDamage = 10
3return DamageModule

And made the Serverscript require this to see if the player has a tool, then set's a local damage variable based on whatever the tool's name is:

01--Gun variables
02local gunType = Player.Character:FindFirstChildOfClass("Tool")
03local name = gunType.Name
04--Damage variables
05local damagesScript = require(script.Parent.Damages)
06local damage
07 
08if Part then
09    --Setting the gun type for the damage of it
10    if name == "Handgun" then
11        damage = damagesScript.handgunDamage
12    --Will have elseif here once more guns are added
13    end
14    --Making the hit entity take damage
15    _G.Hum = Part.Parent:FindFirstChild("Humanoid")
16    if _G.Hum then
17        local localHum = _G.Hum
18        localHum:TakeDamage(damage)
19    end
20end

Answer this question