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 3 years ago
Edited 3 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:

local gun = script.parent
local player = game.Players.LocalPlayer
local clipSize = 15
local maxClipSize = 15
local reloadTime = 2
local damage = 10
--Keys
local UserInputService = game:GetService("UserInputService")
local rKey = Enum.KeyCode.R
-- Obeject Sound Varibles
local gun_sound = gun.Handle['Gun Shot']
local empty_sound = gun.Handle.clip_empty
local reload_sound = gun.Handle.Reload 

gun.Equipped:Connect(function(mouse)

    player.PlayerGui.ScreenGui.Ammo.Visible = true
    --Firing the gun
    mouse.Button1Down:Connect(function()
        if clipSize >= 1 then
            game.ReplicatedStorage.DamageEvent:FireServer(damage)--This is the line to fire the server script
            game.ReplicatedStorage.RayCastEvent:FireServer(script.Parent.Barrel.Position, mouse.Hit.Position)
            gun_sound:Play()
            clipSize -= 1
        else --Reloading the gun automatically after clip is empty
            empty_sound:Play()
            wait(0.01)
            reload_sound:Play()
            wait(reloadTime)
            clipSize = maxClipSize
        end
    end)

    local function IsRKeyPressed()
        return UserInputService:IsKeyDown(rKey)
    end
    --Reload with the "R" key
    local function Input(input, gameProccessedEvent)
        if IsRKeyPressed()then
            reload_sound:Play()
            wait(reloadTime)
            clipSize = maxClipSize
        end
    end
    UserInputService.InputBegan:Connect(Input)
end)

ServerScript:

game.ReplicatedStorage.RayCastEvent.OnServerEvent:Connect(function(Player, FromP, ToP)
    local RayCast = Ray.new(FromP, (ToP-FromP).unit * 100)
    local Part, Position = game.Workspace:FindPartOnRay(RayCast, Player.Character, false, true)
    --Applying the variable
    if Part then
        local Hum = Part.Parent:FindFirstChild("Humanoid")
        if Hum then
            print(_G.damageVariable)

            Hum:TakeDamage(_G.damageVariable)
        end
    end

    local bulletBeam = Instance.new("Part")
    bulletBeam.Parent = game.Workspace
    bulletBeam.CanCollide = false
    bulletBeam.Anchored = true
    local Dist = (ToP-FromP).magnitude
    bulletBeam.CFrame = CFrame.new(FromP, Position) * CFrame.new(0, 0, -Dist / 2)
    bulletBeam.Size = Vector3.new(0.1, 0.1, Dist)
    game.Debris:AddItem(bulletBeam, 0.2)
end)
--Receiving the variable
game.ReplicatedStorage.DamageEvent.OnServerEvent:Connect(function(Player, damage)
    _G.damageVariable = damage
end)
0
You don't. Ziffixture 6913 — 3y
0
There's no way at all I could do it? Hm..that stinks... Real_BlobKing21 0 — 3y

3 answers

Log in to vote
0
Answered by 3 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 — 3y
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 — 3y
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 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

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

local 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()

game.ReplicatedStorage.RemoteEvent:FireServer(player, Variable.Text)

local ^

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

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 — 3y
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 — 3y
Log in to vote
0
Answered by 3 years ago

I created a separate ModuleScript called "Damages" containing:

local DamageModule = {}
    DamageModule.handgunDamage = 10
return 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:

    --Gun variables
    local gunType = Player.Character:FindFirstChildOfClass("Tool")
    local name = gunType.Name
    --Damage variables
    local damagesScript = require(script.Parent.Damages)
    local damage

    if Part then
        --Setting the gun type for the damage of it
        if name == "Handgun" then
            damage = damagesScript.handgunDamage
        --Will have elseif here once more guns are added
        end
        --Making the hit entity take damage
        _G.Hum = Part.Parent:FindFirstChild("Humanoid")
        if _G.Hum then
            local localHum = _G.Hum
            localHum:TakeDamage(damage)
        end
    end

Answer this question