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:
01 | local gun = script.parent |
02 | local player = game.Players.LocalPlayer |
03 | local clipSize = 15 |
04 | local maxClipSize = 15 |
05 | local reloadTime = 2 |
06 | local damage = 10 |
07 | --Keys |
08 | local UserInputService = game:GetService( "UserInputService" ) |
09 | local rKey = Enum.KeyCode.R |
10 | -- Obeject Sound Varibles |
11 | local gun_sound = gun.Handle [ 'Gun Shot' ] |
12 | local empty_sound = gun.Handle.clip_empty |
13 | local reload_sound = gun.Handle.Reload |
14 |
15 | gun.Equipped:Connect( function (mouse) |
ServerScript:
01 | game.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 |
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
1 | 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() |
2 |
3 | game.ReplicatedStorage.RemoteEvent:FireServer(player, Variable.Text) |
local ^
1 | game.ReplicatedStorage.OnServerEvent:Connect( function (player, Variable) |
2 | Variable = "Hello! How are you today" |
3 | wait( 3 ) |
4 | Variable = "" |
5 | 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:
1 | local DamageModule = { } |
2 | DamageModule.handgunDamage = 10 |
3 | 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:
01 | --Gun variables |
02 | local gunType = Player.Character:FindFirstChildOfClass( "Tool" ) |
03 | local name = gunType.Name |
04 | --Damage variables |
05 | local damagesScript = require(script.Parent.Damages) |
06 | local damage |
07 |
08 | if 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 |
20 | end |