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