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)

2 comments
You don't.
There's no way at all I could do it? Hm..that stinks...