I will show the full code, however it is inside a tool if that helps.
local tool = script.Parent local KOValue = "Kills" local player = game:GetService("Players").LocalPlayer local replicatedstorage = game:GetService("ReplicatedStorage") local remoteFunction = replicatedstorage:WaitForChild("RemoteFunction") local gungui = tool.Parent.Parent.PlayerGui:WaitForChild("gungui") local ammo = gungui:WaitForChild("am") local maxammo = gungui:WaitForChild("maxam") local fetchBulletsLeft = replicatedstorage:WaitForChild("BulletsLeft") local UIS = game:GetService("UserInputService") local re = gungui:WaitForChild("reload") local Bullet = replicatedstorage:WaitForChild("bullet") local basepart = game.Workspace.Terrain local bulletspeed = 500 local damage = 25 local FireRate = 0.1 local out = false local attack = false local ko = false local function touching(hit) local zom = hit.Parent.Name == "zombie" if zom then local zombie =hit.Parent.Zombie wait() if zombie.Health <= 0 and ko == false then ko = true local pla = game.Players:GetPlayerFromCharacter(tool.Parent) pla.leaderstats[KOValue].Value = pla.leaderstats[KOValue].Value + 1 wait(0.1) ko = false end end end tool.Activated:Connect(function(hit) if out == false then if attack == false then attack = true local player = game.Players:GetPlayerFromCharacter(tool.Parent) local MouseHitLookVector = remoteFunction:InvokeClient(player) local NewBullet = Bullet:Clone() NewBullet.CFrame = tool.Handle.CFrame NewBullet.Parent = game.Workspace NewBullet.Velocity = MouseHitLookVector * bulletspeed NewBullet.Touched:Connect(touching) if ammo.Value > 0 then ammo.Value -= 1 gungui.Currentammo.Visible = true gungui.ammo.Visible = true elseif maxammo.Value == 0 and ammo.Value == 0 then out = true else re.Visible =true out = true gungui.Currentammo.Visible = false gungui.ammo.Visible = false end wait(FireRate) attack = false basepart.Touched:Connect(function(lit) if lit.Name == "bullet" then wait(2) lit:Destroy() end end) end else UIS.InputBegan:Connect(function(key) print("fuck my life") if key == Enum.KeyCode.R then if re.Visible == true then re.Visible = false wait(2) ammo.Value = 25 maxammo.Value -= 25 out = false end end end) end end)
thankyou for reading, i hope i can fix this :)
See documentation for InputBegan, it returns InputObject, not Enum.KeyCode, you need to replace line 83 with this:
if key.KeyCode == Enum.KeyCode.R then
Though your script still has major issue, everytime Activated
fires and out
is true you are creating a new connection with :Connect
, thus if you activate the tool 20 times with out
being true, you are going to have 20 new connections and all of them will be active, this is bad for performance and memory, fix that by creating only a single connection and check if out
(or other variable you might need) is true / false inside of it. You have done same mistake with basepart.Touched
, though idk what exactly you wanted to achieve with it so I will leave it up to you to fix. Here is an example of what I mean:
UIS.InputBegan:Connect(function(key) -- Stop continuing the code if these conditions are not true -- you should specify your own conditions, toolIsEquipped -- variable does not even exist, just a placeholder if not toolIsEquipped or not out then return end print("fuck my life") if key.KeyCode == Enum.KeyCode.R then if re.Visible == true then re.Visible = false wait(2) ammo.Value = 25 maxammo.Value -= 25 out = false end end end)
Whenever you get better, you can try other method by disconnecting and connecting events.
thankyou for your help, i ended up fixing it just by using a remote function to grab players data, due to the script being a server script.