Cant get the players name from outside a Equipped function

Hey so I’m trying to get the players name from a modular script inside a tool. This script contains all the configurations for the gun and the bullet part.

I’m trying to get the players name when the tool is equipped then turn that info into a line of code in the module

Getting the player itself works just fine but getting the players name out of the equipped function isn’t.

The bullet gets cloned when the player shoots and the modular script inside the gun tool gives the bullet the info it needs.

image

local Tool = script.Parent

Tool.Equipped:Connect(function()
	
local player = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
	
	print(player.Name)
	
	plr = player.Name

end)

local GunInfo = {}


GunInfo.BulletPart = game.ReplicatedStorage:WaitForChild("PistolBullet") -- link to bullet part (can be used for special bullets)
GunInfo.BulletSpeed = 350 -- How fast a bullet travel
GunInfo.FallOffTime = 0.15 -- In seconds: How long a bullet can go without suffering gravity
GunInfo.DestroyOnFallOff = true -- Whether the bullet will delete itself after FallOffTime

----------------------
--  Getting Player  --
----------------------

GunInfo.Player = plr

return GunInfo

image

1 Like
local Tool = script.Parent
local player

Tool.Equipped:Connect(function()
	player = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
	print(player.Name)
end)

local GunInfo = {}

GunInfo.BulletPart = game.ReplicatedStorage:WaitForChild("PistolBullet") -- link to bullet part (can be used for special bullets)
GunInfo.BulletSpeed = 350 -- How fast a bullet travel
GunInfo.FallOffTime = 0.15 -- In seconds: How long a bullet can go without suffering gravity
GunInfo.DestroyOnFallOff = true -- Whether the bullet will delete itself after FallOffTime

----------------------
--  Getting Player  --
----------------------
if player:IsA("Player") then
   GunInfo.Player = player
end

return GunInfo

I tried removing the local on the initial player = but the modular script errors and doesn’t load when I do that

I removed the IsAplayer and it loaded properly

So is your script working? Or if not i will send another

Im still having the same issue as before though

Okay then. So first, even if you call a function in module script it won’t work. That’s why we are calling module from local scripts or server scripts. Here is the script:

--Module
local Tool = script.Parent

local GunInfo = {}

GunInfo.BulletPart = game.ReplicatedStorage:WaitForChild("PistolBullet") -- link to bullet part (can be used for special bullets)
GunInfo.BulletSpeed = 350 -- How fast a bullet travel
GunInfo.FallOffTime = 0.15 -- In seconds: How long a bullet can go without suffering gravity
GunInfo.DestroyOnFallOff = true -- Whether the bullet will delete itself after FallOffTime

GunInfo.GetPlayer = Tool.Equipped:Connect(function()
	local player = game.Players:GetPlayerFromCharacter(script.Parent.Parent) or script.Parent.Parent

	GunInfo.Player = player
end)

print(GunInfo.Player)

return GunInfo
-- ServerSide or you can make with local script
local GunInfo = require(script.Parent.ModuleScript)

GunInfo.GetPlayer()

hmm this almost works fine but when applying the config to the bullet the script breaks.


No issues in the modular script this is fine.

local OriginalDamage = ValueFolder.Damage.Value – All of the configs for the bullet are listed below

NewBullet.Configs.Damage.Value = ValueFolder.Damage.Value
NewBullet.Configs.Headshot.Value = GunInfo.Headshot
NewBullet.Configs.HeadshotMultiplier.Value = GunInfo.HeadshotMultiplier
NewBullet.Configs.BulletDamageFallOff.Value = GunInfo.DamageFallOff
NewBullet.Configs.BulletDamageFallOffDistance.Value = GunInfo.DamageFallOffDistance
NewBullet.BulletVelocity.Velocity = direction * GunInfo.BulletSpeed
NewBullet.Configs.Player = GunInfo.GetPlayer
NewBullet.Parent = workspace["Bullets and Casings"]

This breaks tho

let me check again i got the same problem.

I got it workin thanks to you!!!
Had to mess with it a bit and make an extra value but it works perfectly now!
Thanks for all the help and I hope you have a good day and or night :slight_smile:

Here is the script:

--Module
local Tool = script.Parent

local GunInfo = {}

GunInfo.BulletPart = "blablabla" -- link to bullet part (can be used for special bullets)
GunInfo.BulletSpeed = 350 -- How fast a bullet travel
GunInfo.FallOffTime = 0.15 -- In seconds: How long a bullet can go without suffering gravity
GunInfo.DestroyOnFallOff = true -- Whether the bullet will delete itself after FallOffTime

GunInfo.GetPlayer = Tool.Equipped:Connect(function()
	local player = game.Players:GetPlayerFromCharacter(script.Parent.Parent) or script.Parent.Parent

	GunInfo.Player = player
end)

return GunInfo
--ServerScript
local Module = require(script.Parent.ModuleScript)

local player = Module.Player -- It will say its nil because when you set a variable it gets that current value i recommend dont use variable and say Module.Player

1 Like

Okay, no problem. Dont forget to dont assign player variable to GunInfo.Player because when you assign it, it will be assigned to current value(nil) even if you hold the tool. I recommend using without variable.

1 Like