I’m working on a TPS, and for the guns, I welded the gun to a player’s character, and the player can click to shoot.
The guns themselves are named “Gun” (I’ll change the name later) and have an attachment called “GunFirePoint”. (see explorer below)
A bullet projectile is then fired from the GunFirePoint attachment, and looks toward’s the player’s mouse’s position. The bullet is parented inside this folder:
The gun system works perfectly, but after the player dies, they can’t see the bullet on their screen even though the bullet is still inside the “Projectiles” folder.
This is from playing solo; the client generates the bullets.
Code:
Code
--Services
local Players = game:GetService('Players')
--Player
local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild('Humanoid')
local HumanoidRootPart = Character:WaitForChild('HumanoidRootPart')
--Important Variables
local CanShoot = false
local IsDead = false
local GunSettings
local Gun, GunFirePoint = Character:WaitForChild('Gun'), Character:WaitForChild('Gun'):WaitForChild('GunFirePoint')
--Events
local Mouse = ModuleList.Mouse
local MouseEvent
MouseEvent = Mouse.Button1Down:Connect(function()
if CanShoot then return end
if IsDead == false then
CanShoot = true
local origin = GunFirePoint.WorldPosition
local direction = (Mouse.Hit.p - origin).Unit
local movementSpeed = HumanoidRootPart.Velocity
local speed = (direction * GunSettings.Speed)
local projectileData = {
bulletType = GunSettings.BulletType;
owner = LocalPlayer;
damage = {
['Normal'] = GunSettings.Damage.Normal;
['Headshot'] = GunSettings.Damage.Headshot
};
Pos = Mouse.Hit.p;
GFP = GunFirePoint;
cooldown = GunSettings.Cooldown
}
local castArgs = {
origin = origin;
directionWithMagnitude = direction * 500;
velocity = speed;
blacklist = {Character, game.Workspace.World.Projectiles};
cosmeticBulletObject = nil;
ignoreWater = true;
bulletAcceleration = Vector3.new(0, 0, 0);
canPierceFunction = function(hitPart, hitPoint, normal, material)
if Players:GetPlayerFromCharacter(hitPart.Parent) then
return true
end
return false
end
}
playAnimation(Character, Animations:WaitForChild('Fire'), 2, 1)
local mark = getAnimationSignal('Fire')
mark:Connect(function()
Gun:WaitForChild('Fire'):Play()
ReplicatedStorage.Remotes.Projectiles:WaitForChild('ProjectileServer'):FireServer(projectileData, castArgs)
end)
wait(GunSettings.Cooldown)
CanShoot = false
end
end)
Some things to consider: Are the bullets anchored? Do they have collisions on? Did they move out of range?
Also, doing this on the client is unwise because other players will not be able to see the bullets, and this will make it easy for hackers to do whatever they want.
Are the bullets anchored? Do they have collisions?
Before seeing your post, I saw that the bullets were indeed anchored. I set them to false, and the problem still occurred.
What do you mean by the bullets having collisions? If you’re talking about the CanCollide property for the bullets, I set them to false. As a side note, the bullets do have collisions (ex: players, walls). I’m using FastCast for the bullets.
Also, doing this on the client is unwise because other players will not be able to see the bullets, and this will make it easy for hackers to do whatever they want.
I might tweak this later on, but for now, I think it’s okay. The players do see the bullets, FYI. Here’s what I did:
The person that’s firing the bullets sends the bullet information to the server
The server receives the bullet data and fires another RemoteEvent to all the clients (I might change this later down the line, 'cause it’s a bit risky)
The client receives the information from the server and starts drawing the bullet and fires the bullet’s ray. The hit detection is done on the client; once the ray hits a player, the client fires another RemoteEvent to the server
The server does hit validation: if the bullet is near that specific player, the targeted player will take damage.
There’s a bit of controversy on whether or not you’re supposed to do hit detection on the client or on the server. Originally I chose the latter, but then I decided to go with the client because apparently some developers do hit detection on the client, and validate that hit on the server.
This seems like an issue with FastCast and not your own code. I’d contact the developer to be sure.
I have nothing but respect for the person who made FastCast but, something worth noting is that if you use a library, framework or module (in your case), you’re at the mercy of the developer. The range of freedom and learning you have and can obtain is far better (in my opinion) than the portability & ease-of-use that the module provides for you.
I was experimenting with my own raycast (this was just for testing purposes, that’s why the bullets drop down ), and I discovered this:
Even after I reset, I still cant shoot. I know for a fact that the problem stems from this line below:
local Gun, GunFirePoint = Character:WaitForChild('Gun'), Character:WaitForChild('Gun'):WaitForChild('GunFirePoint')
…but I don’t know how to get it to work.
I messaged the developer as well. One of the things that I haven’t put in the OP is that if I were to destroy the gun part when the player dies, and the same player respawns and tries to shoot, the bullet would fire from the old GunFirePoint. Take a look:
I feel like this is a simple fix, but I just don’t know where to start.
On my phone right now so can’t actually do anything with the scripts, but try adding a print function some places in the code and see if they run after you reset too, this way you can see if the problem is FastCast or your code.
There’s two scripts: one that allows the user to fire the bullet; and one that generates the bullet. Both of them are located inside StarterPlayerScripts
Try putting them in StarterCharacterScripts and see what happens. I remember encountering an issue similar to this where “dead” references were being passed even though the player resets or dies. If I recall correctly, StarterCharacterScripts resets after the player dies. Don’t quote me on that though, I can’t say that for certain.
Thank you so much, @sanjay2003. I moved the script where you fire the bullet inside StarterCharacterScripts, and I don’t know why but it worked!
I think if I put the script inside StarterPlayerScripts, it won’t pass the “dead references” after the player dies as you’ve said before.
Aaugh… figuring out what was wrong with the script was so painful. I kept the script where the client generates the bullet inside StarterPlayerScripts. I don’t think it’ll get problematic any time soon.