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)
Just make a new variable in the server, and get the information from the server.
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!"
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