hello, im making a roblox game, this is the gun sc...
Criado em: 19 de fevereiro de 2025
Criado em: 19 de fevereiro de 2025
hello, im making a roblox game, this is the gun script:
local tool = script.Parent
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local peopleHitStore = DataStoreService:GetDataStore("PeopleHitStore")
local BadgeService = game:GetService("BadgeService")
-- Replace this with your actual Badge ID
local spinningHitBadgeId = 214097955970999
-- Configuration values
local projectileSpeed = 100
local cooldownTime = 1.5
local firingDebounce = false
local knockbackPower = 50
local upwardForce = 0.8
local rotationPower = 5
local ballPushForce = 100
local explosionDelay = 3
local explosionRadius = 10
local tickInterval = 0.5
local function applyKnockbackAndRagdoll(character, hitDirection, origin)
local ragdollEvent = character:WaitForChild("RagdollEvent")
ragdollEvent:Fire(origin)
local rootPart = character:FindFirstChild("HumanoidRootPart")
if rootPart then
local knockback = Instance.new("BodyVelocity")
knockback.MaxForce = Vector3.new(1e5, 1e5, 1e5)
knockback.Velocity = hitDirection * knockbackPower
knockback.Parent = rootPart
textlocal spin = Instance.new("BodyAngularVelocity") spin.AngularVelocity = Vector3.new( math.random(-1, 1) * rotationPower, math.random(-1, 1) * rotationPower, math.random(-1, 1) * rotationPower ) spin.MaxTorque = Vector3.new(1e5, 1e5, 1e5) spin.Parent = rootPart game:GetService("Debris"):AddItem(knockback, 0.5) game:GetService("Debris"):AddItem(spin, 0.5) end
end
local function createProjectile(origin, direction, shooter)
local projectile = Instance.new("Part")
projectile.Shape = Enum.PartType.Ball
projectile.Size = Vector3.new(2, 2, 2)
projectile.CFrame = CFrame.new(origin)
projectile.Color = Color3.new(1, 0, 0)
projectile.Anchored = false
projectile.CanCollide = false
projectile.Parent = workspace
textlocal creator = Instance.new("ObjectValue") creator.Name = "creator" creator.Value = shooter creator.Parent = projectile local bv = Instance.new("BodyVelocity") bv.Velocity = direction * projectileSpeed bv.MaxForce = Vector3.new(1e5, 1e5, 1e5) bv.Parent = projectile local hitRegistered = false projectile.Touched:Connect(function(hit) if hitRegistered then return end -- Check if we hit an ability ball (the bomb) if hit:GetAttribute("IsAbilityBall") then hitRegistered = true -- Push the bomb in the projectile's direction local pushDirection = direction local pushForce = Instance.new("BodyVelocity") pushForce.Velocity = pushDirection * ballPushForce pushForce.MaxForce = Vector3.new(1e5, 1e5, 1e5) pushForce.Parent = hit game:GetService("Debris"):AddItem(pushForce, 0.5) -- Badge awarding logic: simply record the shooter if not already recorded. if not hit:GetAttribute("BadgeShooter") then hit:SetAttribute("BadgeShooter", shooter.UserId) print("Bomb hit recorded shooter:", shooter.UserId) end projectile:Destroy() return end -- If a player character (other than the shooter) is hit, process as before. local character = hit.Parent if character and character:FindFirstChild("Humanoid") and character ~= shooter.Character then hitRegistered = true local targetPlayer = Players:GetPlayerFromCharacter(character) if targetPlayer then if game.PrivateServerOwnerId == 0 then if shooter and shooter:FindFirstChild("leaderstats") and shooter.leaderstats:FindFirstChild("PeopleHit") then shooter.leaderstats.PeopleHit.Value += 1 pcall(function() peopleHitStore:UpdateAsync(shooter.UserId, function(old) return (old or 0) + 1 end) end) end end end local hitDirection = (character.PrimaryPart.Position - origin).Unit hitDirection = hitDirection + Vector3.new(0, upwardForce, 0) applyKnockbackAndRagdoll(character, hitDirection, origin) projectile:Destroy() end end) game:GetService("Debris"):AddItem(projectile, 5)
end
tool.Activated:Connect(function()
if firingDebounce then return end
firingDebounce = true
textlocal character = tool.Parent local head = character:FindFirstChild("Head") local hrp = character:FindFirstChild("HumanoidRootPart") local shooter = Players:GetPlayerFromCharacter(character) if head and hrp and shooter then local origin = head.Position local direction = hrp.CFrame.LookVector createProjectile(origin, direction, shooter) end wait(cooldownTime) firingDebounce = false
end)
local abilityEvent = tool:WaitForChild("AbilityEvent")
local abilityCooldown = 5
local lastAbilitySpawn = {}
abilityEvent.OnServerEvent:Connect(function(player)
local currentTime = tick()
if lastAbilitySpawn[player.UserId] and (currentTime - lastAbilitySpawn[player.UserId] < abilityCooldown) then
return
end
lastAbilitySpawn[player.UserId] = currentTime
textlocal character = player.Character if character and character.PrimaryPart then local sphere = Instance.new("Part") sphere.Shape = Enum.PartType.Ball sphere.Size = Vector3.new(4, 4, 4) sphere.CFrame = character.PrimaryPart.CFrame * CFrame.new(0, 5, -4) sphere.Color = Color3.new(0, 0, 0) -- Start as black sphere.Anchored = false sphere.CanCollide = true sphere.CustomPhysicalProperties = PhysicalProperties.new(50, 0.3, 0.5, 1, 1) sphere:SetAttribute("IsAbilityBall", true) -- Initialize attributes for badge awarding. sphere:SetAttribute("BadgeShooter", nil) sphere:SetAttribute("BadgeAwarded", false) sphere:SetAttribute("IsSpinning", false) sphere.Parent = workspace -- Ticking color effect: alternate between red and black every tickInterval seconds. spawn(function() local toggle = false local startTime = tick() while tick() - startTime < explosionDelay and sphere and sphere.Parent do if toggle then sphere.Color = Color3.new(1, 0, 0) -- Red sphere:SetAttribute("IsSpinning", true) else sphere.Color = Color3.new(0, 0, 0) -- Black sphere:SetAttribute("IsSpinning", false) end toggle = not toggle wait(tickInterval) end end) local bombKnockbackForce = 150 -- Increase this value to make the bomb stronger -- Schedule explosion after a delay. spawn(function() wait(explosionDelay) if sphere and sphere.Parent then local explosion = Instance.new("Explosion") explosion.Position = sphere.Position explosion.BlastRadius = explosionRadius explosion.BlastPressure = 0 -- No damage explosion.Parent = workspace -- Check for players in range and apply ragdoll + knockback. local hitSomeone = false for _, player in pairs(Players:GetPlayers()) do local char = player.Character if char and char:FindFirstChild("HumanoidRootPart") and char:FindFirstChild("RagdollEvent") then local distance = (char.HumanoidRootPart.Position - sphere.Position).Magnitude if distance <= explosionRadius then hitSomeone = true char.RagdollEvent:Fire(sphere.Position) local knockback = Instance.new("BodyVelocity") local direction = (char.HumanoidRootPart.Position - sphere.Position).Unit knockback.Velocity = direction * bombKnockbackForce knockback.MaxForce = Vector3.new(1e5, 1e5, 1e5) knockback.Parent = char.HumanoidRootPart game:GetService("Debris"):AddItem(knockback, 0.5) end end end -- Award badge only if the explosion hit at least one player. if hitSomeone then local badgeShooter = sphere:GetAttribute("BadgeShooter") if badgeShooter and (not sphere:GetAttribute("BadgeAwarded")) then sphere:SetAttribute("BadgeAwarded", true) print("Explosion affected a player; awarding badge to shooter:", badgeShooter) pcall(function() BadgeService:AwardBadge(badgeShooter, spinningHitBadgeId) end) end end sphere:Destroy() end end) game:GetService("Debris"):AddItem(sphere, explosionDelay + 1) end
end)
i also made a localscript that activates the ability:
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local tool = script.Parent
local abilityEvent = tool:WaitForChild("AbilityEvent") -- RemoteEvent for bomb ability
-- Ability cooldown settings (adjust as desired)
local bombCooldownTime = 7
local lastBombActivationTime = 0
-- Create a ScreenGui for the bomb ability GUI
local screenGui = Instance.new("ScreenGui")
screenGui.Name = "BombAbilityGui"
screenGui.Parent = player:WaitForChild("PlayerGui")
-- Create a TextLabel to display the cooldown countdown
local cooldownLabel = Instance.new("TextLabel")
cooldownLabel.Name = "CooldownLabel"
cooldownLabel.Size = UDim2.new(0, 120, 0, 50)
cooldownLabel.Position = UDim2.new(0.9, -60, 0.5, 100) -- Adjust position as needed
cooldownLabel.BackgroundTransparency = 0.5
cooldownLabel.BackgroundColor3 = Color3.new(0, 0, 0)
cooldownLabel.TextColor3 = Color3.new(1, 1, 1)
cooldownLabel.TextScaled = true
cooldownLabel.Visible = false
cooldownLabel.Parent = screenGui
-- Function to update the cooldown display
local function startBombCooldownDisplay()
cooldownLabel.Visible = true
local remaining = bombCooldownTime
while remaining > 0 do
cooldownLabel.Text = "Cooldown: " .. math.ceil(remaining) .. "s"
wait(1)
remaining = remaining - 1
end
cooldownLabel.Visible = false
end
-- (Optional) Mobile button for bomb ability
local mobileButton
if UserInputService.TouchEnabled then
mobileButton = Instance.new("ImageButton")
mobileButton.Name = "BombAbilityButton"
mobileButton.Size = UDim2.new(0, 80, 0, 80) -- Adjust size as needed
mobileButton.Position = UDim2.new(1, -120, 0, 0) -- Position on the right side of the screen
mobileButton.Image = "rbxassetid://101720181436564" -- Replace with your custom button image asset ID
mobileButton.BackgroundTransparency = 1
mobileButton.Visible = false
mobileButton.Parent = screenGui
texttool.Equipped:Connect(function() mobileButton.Visible = true end) tool.Unequipped:Connect(function() mobileButton.Visible = false end) mobileButton.MouseButton1Click:Connect(function() local currentTime = tick() if currentTime - lastBombActivationTime >= bombCooldownTime then lastBombActivationTime = currentTime abilityEvent:FireServer() spawn(startBombCooldownDisplay) print("Bomb ability activated on mobile!") else print("Bomb ability is on cooldown!") end end)
end
-- PC input: Listen for the E key when the tool is equipped
local keyConn
tool.Equipped:Connect(function()
keyConn = UserInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.E then
local currentTime = tick()
if currentTime - lastBombActivationTime >= bombCooldownTime then
lastBombActivationTime = currentTime
abilityEvent:FireServer()
spawn(startBombCooldownDisplay)
print("Bomb ability activated on PC!")
else
print("Bomb ability is on cooldown!")
end
end
end)
end)
tool.Unequipped:Connect(function()
if keyConn then
keyConn:Disconnect()
keyConn = nil
end
end)
so, can you help me make a new gun that spawns a banana when i press e, modify the localscript to have a trigger for the ability and add the ability functionality in the gun script, also, remove the badges and the already existing ability
Below are two updated scripts that remove the old bomb ability and badge logic and instead add a new “banana ability.” When the player presses E (or taps the mobile button), a remote event fires to the server, which spawns a banana in front of the player. (Be sure to replace the placeholder asset IDs with your actual banana mesh/texture asset IDs if you have them.)
Place this script in your gun tool (under ServerScriptService or as a child of the tool):
lualocal tool = script.Parent local Players = game:GetService("Players") local Debris = game:GetService("Debris") local projectileSpeed = 100 local cooldownTime = 1.5 local firingDebounce = false --------------------------------------------------------- -- Function to create and fire the projectile (shooting) --------------------------------------------------------- local function createProjectile(origin, direction, shooter) local projectile = Instance.new("Part") projectile.Shape = Enum.PartType.Ball projectile.Size = Vector3.new(2, 2, 2) projectile.CFrame = CFrame.new(origin) projectile.Color = Color3.new(1, 0, 0) projectile.Anchored = false projectile.CanCollide = false projectile.Parent = workspace local creator = Instance.new("ObjectValue") creator.Name = "creator" creator.Value = shooter creator.Parent = projectile local bv = Instance.new("BodyVelocity") bv.Velocity = direction * projectileSpeed bv.MaxForce = Vector3.new(1e5, 1e5, 1e5) bv.Parent = projectile projectile.Touched:Connect(function(hit) local character = hit.Parent if character and character:FindFirstChild("Humanoid") and character ~= shooter.Character then local hrp = character:FindFirstChild("HumanoidRootPart") if hrp then local hitDirection = (hrp.Position - origin).Unit local knockback = Instance.new("BodyVelocity") knockback.MaxForce = Vector3.new(1e5, 1e5, 1e5) knockback.Velocity = hitDirection * 50 knockback.Parent = hrp Debris:AddItem(knockback, 0.5) end projectile:Destroy() end end) Debris:AddItem(projectile, 5) end --------------------------------------------------------- -- Tool activation (shooting) --------------------------------------------------------- tool.Activated:Connect(function() if firingDebounce then return end firingDebounce = true local character = tool.Parent local head = character:FindFirstChild("Head") local hrp = character:FindFirstChild("HumanoidRootPart") local shooter = Players:GetPlayerFromCharacter(character) if head and hrp and shooter then local origin = head.Position local direction = hrp.CFrame.LookVector createProjectile(origin, direction, shooter) end wait(cooldownTime) firingDebounce = false end) --------------------------------------------------------- -- Banana ability: Spawns a banana when activated --------------------------------------------------------- local abilityEvent = tool:WaitForChild("AbilityEvent") -- RemoteEvent inside the tool abilityEvent.OnServerEvent:Connect(function(player) local character = player.Character if character and character.PrimaryPart then -- Create a banana part local banana = Instance.new("Part") banana.Name = "Banana" banana.Size = Vector3.new(2, 1, 6) banana.CFrame = character.PrimaryPart.CFrame * CFrame.new(0, 0, -5) banana.Color = Color3.new(1, 1, 0) -- Yellow banana.Anchored = false banana.CanCollide = true banana.Parent = workspace -- Optional: add a Mesh to make it look like a banana. local mesh = Instance.new("SpecialMesh", banana) mesh.MeshType = Enum.MeshType.FileMesh mesh.MeshId = "rbxassetid://INSERT_BANANA_MESH_ASSET_ID" -- Replace with your banana mesh asset ID mesh.TextureId = "rbxassetid://INSERT_BANANA_TEXTURE_ASSET_ID" -- (Optional) Replace with your texture asset ID mesh.Scale = Vector3.new(1, 1, 1) -- Toss the banana forward with a bit of upward force. local bv = Instance.new("BodyVelocity") bv.Velocity = character.PrimaryPart.CFrame.LookVector * 50 + Vector3.new(0, 20, 0) bv.MaxForce = Vector3.new(1e5, 1e5, 1e5) bv.Parent = banana Debris:AddItem(bv, 1) -- Clean up the banana after 10 seconds. Debris:AddItem(banana, 10) end end)
Place this LocalScript as a child of the tool. It sets up both PC and mobile input (pressing E or tapping the mobile button) to fire the banana ability:
lualocal UserInputService = game:GetService("UserInputService") local Players = game:GetService("Players") local player = Players.LocalPlayer local tool = script.Parent local abilityEvent = tool:WaitForChild("AbilityEvent") -- RemoteEvent for banana ability -- Ability cooldown settings local bananaCooldownTime = 7 local lastBananaActivationTime = 0 -- Create a ScreenGui for the banana ability GUI local screenGui = Instance.new("ScreenGui") screenGui.Name = "BananaAbilityGui" screenGui.Parent = player:WaitForChild("PlayerGui") -- Create a TextLabel to display the cooldown countdown local cooldownLabel = Instance.new("TextLabel") cooldownLabel.Name = "CooldownLabel" cooldownLabel.Size = UDim2.new(0, 120, 0, 50) cooldownLabel.Position = UDim2.new(0.9, -60, 0.5, 100) cooldownLabel.BackgroundTransparency = 0.5 cooldownLabel.BackgroundColor3 = Color3.new(0, 0, 0) cooldownLabel.TextColor3 = Color3.new(1, 1, 1) cooldownLabel.TextScaled = true cooldownLabel.Visible = false cooldownLabel.Parent = screenGui -- Function to update the cooldown display local function startBananaCooldownDisplay() cooldownLabel.Visible = true local remaining = bananaCooldownTime while remaining > 0 do cooldownLabel.Text = "Cooldown: " .. math.ceil(remaining) .. "s" wait(1) remaining = remaining - 1 end cooldownLabel.Visible = false end -- Mobile button for banana ability (if touch enabled) local mobileButton if UserInputService.TouchEnabled then mobileButton = Instance.new("ImageButton") mobileButton.Name = "BananaAbilityButton" mobileButton.Size = UDim2.new(0, 80, 0, 80) mobileButton.Position = UDim2.new(1, -120, 0, 0) mobileButton.Image = "rbxassetid://101720181436564" -- Replace with your custom button image asset ID mobileButton.BackgroundTransparency = 1 mobileButton.Visible = false mobileButton.Parent = screenGui tool.Equipped:Connect(function() mobileButton.Visible = true end) tool.Unequipped:Connect(function() mobileButton.Visible = false end) mobileButton.MouseButton1Click:Connect(function() local currentTime = tick() if currentTime - lastBananaActivationTime >= bananaCooldownTime then lastBananaActivationTime = currentTime abilityEvent:FireServer() spawn(startBananaCooldownDisplay) print("Banana ability activated on mobile!") else print("Banana ability is on cooldown!") end end) end -- PC input: Listen for the E key when the tool is equipped local keyConn tool.Equipped:Connect(function() keyConn = UserInputService.InputBegan:Connect(function(input, gameProcessed) if gameProcessed then return end if input.KeyCode == Enum.KeyCode.E then local currentTime = tick() if currentTime - lastBananaActivationTime >= bananaCooldownTime then lastBananaActivationTime = currentTime abilityEvent:FireServer() spawn(startBananaCooldownDisplay) print("Banana ability activated on PC!") else print("Banana ability is on cooldown!") end end end) end) tool.Unequipped:Connect(function() if keyConn then keyConn:Disconnect() keyConn = nil end end)
Customize values (such as speeds, offsets, cooldowns, and asset IDs) as needed for your game.
make so that the banana always spawns on the ground
Below is an updated version of the server-side script for your gun tool. In this version, the banana ability now computes a spawn position on the ground directly beneath (or near) the player instead of tossing it into the air. The banana is spawned by casting a ray downward from a desired offset from the player; if the ray hits something (such as the terrain), the banana is positioned at that hit point.
Replace the old ability code in your server script with the code below:
lualocal tool = script.Parent local Players = game:GetService("Players") local Debris = game:GetService("Debris") local projectileSpeed = 100 local cooldownTime = 1.5 local firingDebounce = false --------------------------------------------------------- -- Function to create and fire the projectile (shooting) --------------------------------------------------------- local function createProjectile(origin, direction, shooter) local projectile = Instance.new("Part") projectile.Shape = Enum.PartType.Ball projectile.Size = Vector3.new(2, 2, 2) projectile.CFrame = CFrame.new(origin) projectile.Color = Color3.new(1, 0, 0) projectile.Anchored = false projectile.CanCollide = false projectile.Parent = workspace local creator = Instance.new("ObjectValue") creator.Name = "creator" creator.Value = shooter creator.Parent = projectile local bv = Instance.new("BodyVelocity") bv.Velocity = direction * projectileSpeed bv.MaxForce = Vector3.new(1e5, 1e5, 1e5) bv.Parent = projectile projectile.Touched:Connect(function(hit) local character = hit.Parent if character and character:FindFirstChild("Humanoid") and character ~= shooter.Character then local hrp = character:FindFirstChild("HumanoidRootPart") if hrp then local hitDirection = (hrp.Position - origin).Unit local knockback = Instance.new("BodyVelocity") knockback.MaxForce = Vector3.new(1e5, 1e5, 1e5) knockback.Velocity = hitDirection * 50 knockback.Parent = hrp Debris:AddItem(knockback, 0.5) end projectile:Destroy() end end) Debris:AddItem(projectile, 5) end --------------------------------------------------------- -- Tool activation (shooting) --------------------------------------------------------- tool.Activated:Connect(function() if firingDebounce then return end firingDebounce = true local character = tool.Parent local head = character:FindFirstChild("Head") local hrp = character:FindFirstChild("HumanoidRootPart") local shooter = Players:GetPlayerFromCharacter(character) if head and hrp and shooter then local origin = head.Position local direction = hrp.CFrame.LookVector createProjectile(origin, direction, shooter) end wait(cooldownTime) firingDebounce = false end) --------------------------------------------------------- -- Banana ability: Spawns a banana on the ground when activated --------------------------------------------------------- local abilityEvent = tool:WaitForChild("AbilityEvent") -- RemoteEvent inside the tool abilityEvent.OnServerEvent:Connect(function(player) local character = player.Character if character and character.PrimaryPart then -- Compute a desired spawn offset (in this case, 5 studs ahead) local offset = character.PrimaryPart.CFrame.LookVector * 5 local desiredPosition = character.PrimaryPart.Position + offset -- Cast a ray downward from above the desired position to find the ground local rayOrigin = desiredPosition + Vector3.new(0, 5, 0) local rayDirection = Vector3.new(0, -50, 0) local raycastResult = workspace:Raycast(rayOrigin, rayDirection) if raycastResult then desiredPosition = raycastResult.Position end -- Create the banana at the ground position local banana = Instance.new("Part") banana.Name = "Banana" banana.Size = Vector3.new(2, 1, 6) banana.CFrame = CFrame.new(desiredPosition) banana.Color = Color3.new(1, 1, 0) -- Yellow banana.Anchored = false banana.CanCollide = true banana.Parent = workspace -- Optional: add a Mesh to give the banana a more realistic shape. local mesh = Instance.new("SpecialMesh", banana) mesh.MeshType = Enum.MeshType.FileMesh mesh.MeshId = "rbxassetid://INSERT_BANANA_MESH_ASSET_ID" -- Replace with your banana mesh asset ID mesh.TextureId = "rbxassetid://INSERT_BANANA_TEXTURE_ASSET_ID" -- (Optional) Replace with your texture asset ID mesh.Scale = Vector3.new(1, 1, 1) -- Clean up the banana after 10 seconds. Debris:AddItem(banana, 10) end end)
Determining the Spawn Position:
Spawning the Banana on the Ground:
Cleanup:
Remember to replace the placeholder asset IDs with your actual banana mesh and texture IDs if you have them. Also, adjust the offset or raycast parameters as needed to best suit your game’s terrain.
Finally, your LocalScript (for triggering the ability) can remain unchanged from the previous version since it already fires the remote event on pressing E or clicking the mobile button.
make so that, the banana triggers the RagdollEvent when touched, also make so that it uses the Banana part in replicatedstorage
Below is the updated server‐side script. In this version, when the ability is activated the script clones a pre-made Banana part from ReplicatedStorage (ensure a Banana part exists there) and positions it on the ground. In addition, the banana now listens for touches; if a character with a RagdollEvent touches it, the banana fires that event and then removes itself.
lualocal tool = script.Parent local Players = game:GetService("Players") local Debris = game:GetService("Debris") local ReplicatedStorage = game:GetService("ReplicatedStorage") local projectileSpeed = 100 local cooldownTime = 1.5 local firingDebounce = false --------------------------------------------------------- -- Function to create and fire the projectile (shooting) --------------------------------------------------------- local function createProjectile(origin, direction, shooter) local projectile = Instance.new("Part") projectile.Shape = Enum.PartType.Ball projectile.Size = Vector3.new(2, 2, 2) projectile.CFrame = CFrame.new(origin) projectile.Color = Color3.new(1, 0, 0) projectile.Anchored = false projectile.CanCollide = false projectile.Parent = workspace local creator = Instance.new("ObjectValue") creator.Name = "creator" creator.Value = shooter creator.Parent = projectile local bv = Instance.new("BodyVelocity") bv.Velocity = direction * projectileSpeed bv.MaxForce = Vector3.new(1e5, 1e5, 1e5) bv.Parent = projectile projectile.Touched:Connect(function(hit) local character = hit.Parent if character and character:FindFirstChild("Humanoid") and character ~= shooter.Character then local hrp = character:FindFirstChild("HumanoidRootPart") if hrp then local hitDirection = (hrp.Position - origin).Unit local knockback = Instance.new("BodyVelocity") knockback.MaxForce = Vector3.new(1e5, 1e5, 1e5) knockback.Velocity = hitDirection * 50 knockback.Parent = hrp Debris:AddItem(knockback, 0.5) end projectile:Destroy() end end) Debris:AddItem(projectile, 5) end --------------------------------------------------------- -- Tool activation (shooting) --------------------------------------------------------- tool.Activated:Connect(function() if firingDebounce then return end firingDebounce = true local character = tool.Parent local head = character:FindFirstChild("Head") local hrp = character:FindFirstChild("HumanoidRootPart") local shooter = Players:GetPlayerFromCharacter(character) if head and hrp and shooter then local origin = head.Position local direction = hrp.CFrame.LookVector createProjectile(origin, direction, shooter) end wait(cooldownTime) firingDebounce = false end) --------------------------------------------------------- -- Banana ability: Clones a Banana from ReplicatedStorage, -- spawns it on the ground, and triggers RagdollEvent when touched. --------------------------------------------------------- local abilityEvent = tool:WaitForChild("AbilityEvent") -- RemoteEvent inside the tool abilityEvent.OnServerEvent:Connect(function(player) local character = player.Character if character and character.PrimaryPart then -- Compute a desired spawn offset (e.g. 5 studs ahead of the character) local offset = character.PrimaryPart.CFrame.LookVector * 5 local desiredPosition = character.PrimaryPart.Position + offset -- Cast a ray downward from above the desired position to find the ground. local rayOrigin = desiredPosition + Vector3.new(0, 5, 0) local rayDirection = Vector3.new(0, -50, 0) local raycastResult = workspace:Raycast(rayOrigin, rayDirection) if raycastResult then desiredPosition = raycastResult.Position end -- Clone the Banana part from ReplicatedStorage. local bananaTemplate = ReplicatedStorage:WaitForChild("Banana") local banana = bananaTemplate:Clone() banana.Name = "Banana" banana.CFrame = CFrame.new(desiredPosition) banana.Parent = workspace -- Optionally, ensure the banana is not anchored so that physics can affect it. banana.Anchored = false banana.CanCollide = true -- Add a Touched event to trigger RagdollEvent on characters. local touched = false banana.Touched:Connect(function(hit) if touched then return end local hitCharacter = hit.Parent if hitCharacter and hitCharacter:FindFirstChild("Humanoid") and hitCharacter:FindFirstChild("RagdollEvent") then touched = true hitCharacter.RagdollEvent:Fire(banana.Position) banana:Destroy() end end) -- Cleanup banana after 10 seconds if not already destroyed. Debris:AddItem(banana, 10) end end)
Cloning the Banana:
The script now uses a Banana part stored in ReplicatedStorage. When the ability is triggered, it clones that template rather than creating a new part from scratch.
Spawning on the Ground:
The script calculates a desired position in front of the player and then casts a ray downward to determine the ground position. The banana is then placed at that hit location.
Triggering RagdollEvent:
A Touched event is attached to the banana. When a character with a Humanoid and a RagdollEvent touches the banana, it fires the RagdollEvent (using the banana's position as a parameter) and destroys the banana immediately. A debounce variable ensures the event fires only once per banana.
Remember to create and store your Banana part inside ReplicatedStorage (with any meshes or textures you desire) so the script can successfully clone it.
add the knockback and the other valuables from the original gun script
Below is the updated server‐side script that now includes the knockback, upward force, and angular spin logic from your original gun script. In this version, when the banana (cloned from ReplicatedStorage) is touched by a character, the script calculates a knockback direction (with an added upward force) and fires that character’s RagdollEvent while applying both linear and angular forces before destroying the banana.
Make sure you have a pre-made “Banana” part in ReplicatedStorage.
lualocal tool = script.Parent local Players = game:GetService("Players") local Debris = game:GetService("Debris") local ReplicatedStorage = game:GetService("ReplicatedStorage") -- Configuration values (from original script) local projectileSpeed = 100 local cooldownTime = 1.5 local firingDebounce = false local knockbackPower = 50 local upwardForce = 0.8 local rotationPower = 5 --------------------------------------------------------- -- Function to apply knockback and trigger ragdoll --------------------------------------------------------- local function applyKnockbackAndRagdoll(character, hitDirection, origin) local ragdollEvent = character:WaitForChild("RagdollEvent") ragdollEvent:Fire(origin) local rootPart = character:FindFirstChild("HumanoidRootPart") if rootPart then local knockback = Instance.new("BodyVelocity") knockback.MaxForce = Vector3.new(1e5, 1e5, 1e5) knockback.Velocity = hitDirection * knockbackPower knockback.Parent = rootPart local spin = Instance.new("BodyAngularVelocity") spin.AngularVelocity = Vector3.new( math.random(-1, 1) * rotationPower, math.random(-1, 1) * rotationPower, math.random(-1, 1) * rotationPower ) spin.MaxTorque = Vector3.new(1e5, 1e5, 1e5) spin.Parent = rootPart Debris:AddItem(knockback, 0.5) Debris:AddItem(spin, 0.5) end end --------------------------------------------------------- -- Function to create and fire the projectile (shooting) --------------------------------------------------------- local function createProjectile(origin, direction, shooter) local projectile = Instance.new("Part") projectile.Shape = Enum.PartType.Ball projectile.Size = Vector3.new(2, 2, 2) projectile.CFrame = CFrame.new(origin) projectile.Color = Color3.new(1, 0, 0) projectile.Anchored = false projectile.CanCollide = false projectile.Parent = workspace local creator = Instance.new("ObjectValue") creator.Name = "creator" creator.Value = shooter creator.Parent = projectile local bv = Instance.new("BodyVelocity") bv.Velocity = direction * projectileSpeed bv.MaxForce = Vector3.new(1e5, 1e5, 1e5) bv.Parent = projectile projectile.Touched:Connect(function(hit) local character = hit.Parent if character and character:FindFirstChild("Humanoid") and character ~= shooter.Character then local hrp = character:FindFirstChild("HumanoidRootPart") if hrp then local hitDirection = (hrp.Position - origin).Unit applyKnockbackAndRagdoll(character, hitDirection, origin) end projectile:Destroy() end end) Debris:AddItem(projectile, 5) end --------------------------------------------------------- -- Tool activation (shooting) --------------------------------------------------------- tool.Activated:Connect(function() if firingDebounce then return end firingDebounce = true local character = tool.Parent local head = character:FindFirstChild("Head") local hrp = character:FindFirstChild("HumanoidRootPart") local shooter = Players:GetPlayerFromCharacter(character) if head and hrp and shooter then local origin = head.Position local direction = hrp.CFrame.LookVector createProjectile(origin, direction, shooter) end wait(cooldownTime) firingDebounce = false end) --------------------------------------------------------- -- Banana ability: Clone a Banana from ReplicatedStorage, -- spawn it on the ground, and trigger knockback & RagdollEvent when touched. --------------------------------------------------------- local abilityEvent = tool:WaitForChild("AbilityEvent") -- RemoteEvent inside the tool abilityEvent.OnServerEvent:Connect(function(player) local character = player.Character if character and character.PrimaryPart then -- Compute a desired spawn offset (e.g. 5 studs ahead) local offset = character.PrimaryPart.CFrame.LookVector * 5 local desiredPosition = character.PrimaryPart.Position + offset -- Cast a ray downward from above the desired position to find the ground. local rayOrigin = desiredPosition + Vector3.new(0, 5, 0) local rayDirection = Vector3.new(0, -50, 0) local raycastResult = workspace:Raycast(rayOrigin, rayDirection) if raycastResult then desiredPosition = raycastResult.Position end -- Clone the Banana part from ReplicatedStorage. local bananaTemplate = ReplicatedStorage:WaitForChild("Banana") local banana = bananaTemplate:Clone() banana.Name = "Banana" banana.CFrame = CFrame.new(desiredPosition) banana.Parent = workspace -- Ensure the banana is affected by physics. banana.Anchored = false banana.CanCollide = true -- Add a Touched event to trigger knockback and RagdollEvent on characters. local touched = false banana.Touched:Connect(function(hit) if touched then return end local hitCharacter = hit.Parent if hitCharacter and hitCharacter:FindFirstChild("Humanoid") and hitCharacter:FindFirstChild("RagdollEvent") then touched = true local hrp = hitCharacter:FindFirstChild("HumanoidRootPart") if hrp then local hitDirection = (hrp.Position - banana.Position).Unit -- Add upward force to the hit direction. hitDirection = (hitDirection + Vector3.new(0, upwardForce, 0)).Unit applyKnockbackAndRagdoll(hitCharacter, hitDirection, banana.Position) else -- Fallback: simply fire the ragdoll event. hitCharacter.RagdollEvent:Fire(banana.Position) end banana:Destroy() end end) -- Cleanup banana after 10 seconds if not already triggered. Debris:AddItem(banana, 10) end end)
Knockback Function:
The script now includes the original applyKnockbackAndRagdoll
function. It uses the configured values for knockback power, upward force, and rotation power.
Projectile Function:
The shooting function remains unchanged except that when a projectile hits a valid target, it now calls applyKnockbackAndRagdoll
on that character.
Banana Ability:
applyKnockbackAndRagdoll
to apply both knockback and an angular spin.Adjust any values as needed to best fit your game’s physics and gameplay requirements.
add the peoplehitscore thing as well
Below is the updated server‐side script. In this version, the banana ability uses a Banana part stored in ReplicatedStorage. When the banana is touched by a valid character, it not only triggers the knockback, upward force, and angular spin (via RagdollEvent) but also increments the shooter’s PeopleHit score (updating both the leaderstats and DataStore). Make sure that you have a pre-made Banana part inside ReplicatedStorage and that your leaderstats include a “PeopleHit” value.
lualocal tool = script.Parent local Players = game:GetService("Players") local Debris = game:GetService("Debris") local ReplicatedStorage = game:GetService("ReplicatedStorage") local DataStoreService = game:GetService("DataStoreService") local peopleHitStore = DataStoreService:GetDataStore("PeopleHitStore") -- Configuration values (from original script) local projectileSpeed = 100 local cooldownTime = 1.5 local firingDebounce = false local knockbackPower = 50 local upwardForce = 0.8 local rotationPower = 5 --------------------------------------------------------- -- Function to apply knockback and trigger ragdoll --------------------------------------------------------- local function applyKnockbackAndRagdoll(character, hitDirection, origin) local ragdollEvent = character:WaitForChild("RagdollEvent") ragdollEvent:Fire(origin) local rootPart = character:FindFirstChild("HumanoidRootPart") if rootPart then local knockback = Instance.new("BodyVelocity") knockback.MaxForce = Vector3.new(1e5, 1e5, 1e5) knockback.Velocity = hitDirection * knockbackPower knockback.Parent = rootPart local spin = Instance.new("BodyAngularVelocity") spin.AngularVelocity = Vector3.new( math.random(-1, 1) * rotationPower, math.random(-1, 1) * rotationPower, math.random(-1, 1) * rotationPower ) spin.MaxTorque = Vector3.new(1e5, 1e5, 1e5) spin.Parent = rootPart Debris:AddItem(knockback, 0.5) Debris:AddItem(spin, 0.5) end end --------------------------------------------------------- -- Function to create and fire the projectile (shooting) --------------------------------------------------------- local function createProjectile(origin, direction, shooter) local projectile = Instance.new("Part") projectile.Shape = Enum.PartType.Ball projectile.Size = Vector3.new(2, 2, 2) projectile.CFrame = CFrame.new(origin) projectile.Color = Color3.new(1, 0, 0) projectile.Anchored = false projectile.CanCollide = false projectile.Parent = workspace local creator = Instance.new("ObjectValue") creator.Name = "creator" creator.Value = shooter creator.Parent = projectile local bv = Instance.new("BodyVelocity") bv.Velocity = direction * projectileSpeed bv.MaxForce = Vector3.new(1e5, 1e5, 1e5) bv.Parent = projectile projectile.Touched:Connect(function(hit) local character = hit.Parent if character and character:FindFirstChild("Humanoid") and character ~= shooter.Character then local hrp = character:FindFirstChild("HumanoidRootPart") if hrp then local hitDirection = (hrp.Position - origin).Unit applyKnockbackAndRagdoll(character, hitDirection, origin) end -- PeopleHit score update for projectile hits. if game.PrivateServerOwnerId == 0 and shooter and shooter:FindFirstChild("leaderstats") and shooter.leaderstats:FindFirstChild("PeopleHit") then shooter.leaderstats.PeopleHit.Value = shooter.leaderstats.PeopleHit.Value + 1 pcall(function() peopleHitStore:UpdateAsync(shooter.UserId, function(old) return (old or 0) + 1 end) end) end projectile:Destroy() end end) Debris:AddItem(projectile, 5) end --------------------------------------------------------- -- Tool activation (shooting) --------------------------------------------------------- tool.Activated:Connect(function() if firingDebounce then return end firingDebounce = true local character = tool.Parent local head = character:FindFirstChild("Head") local hrp = character:FindFirstChild("HumanoidRootPart") local shooter = Players:GetPlayerFromCharacter(character) if head and hrp and shooter then local origin = head.Position local direction = hrp.CFrame.LookVector createProjectile(origin, direction, shooter) end wait(cooldownTime) firingDebounce = false end) --------------------------------------------------------- -- Banana ability: Clone a Banana from ReplicatedStorage, -- spawn it on the ground, and trigger knockback, ragdoll, and PeopleHit score update when touched. --------------------------------------------------------- local abilityEvent = tool:WaitForChild("AbilityEvent") -- RemoteEvent inside the tool abilityEvent.OnServerEvent:Connect(function(player) local character = player.Character if character and character.PrimaryPart then -- Compute a desired spawn offset (e.g. 5 studs ahead) local offset = character.PrimaryPart.CFrame.LookVector * 5 local desiredPosition = character.PrimaryPart.Position + offset -- Cast a ray downward from above the desired position to find the ground. local rayOrigin = desiredPosition + Vector3.new(0, 5, 0) local rayDirection = Vector3.new(0, -50, 0) local raycastResult = workspace:Raycast(rayOrigin, rayDirection) if raycastResult then desiredPosition = raycastResult.Position end -- Clone the Banana part from ReplicatedStorage. local bananaTemplate = ReplicatedStorage:WaitForChild("Banana") local banana = bananaTemplate:Clone() banana.Name = "Banana" banana.CFrame = CFrame.new(desiredPosition) banana.Parent = workspace -- Ensure the banana is affected by physics. banana.Anchored = false banana.CanCollide = true -- Add a Touched event to trigger knockback, ragdoll, and PeopleHit score update. local touched = false banana.Touched:Connect(function(hit) if touched then return end local hitCharacter = hit.Parent if hitCharacter and hitCharacter:FindFirstChild("Humanoid") and hitCharacter:FindFirstChild("RagdollEvent") then touched = true local hrp = hitCharacter:FindFirstChild("HumanoidRootPart") local hitDirection = Vector3.new(0, 0, 0) if hrp then hitDirection = (hrp.Position - banana.Position).Unit -- Add upward force to the hit direction. hitDirection = (hitDirection + Vector3.new(0, upwardForce, 0)).Unit applyKnockbackAndRagdoll(hitCharacter, hitDirection, banana.Position) else hitCharacter.RagdollEvent:Fire(banana.Position) end -- PeopleHit score update for banana ability. local shooter = player if game.PrivateServerOwnerId == 0 and shooter and shooter:FindFirstChild("leaderstats") and shooter.leaderstats:FindFirstChild("PeopleHit") then shooter.leaderstats.PeopleHit.Value = shooter.leaderstats.PeopleHit.Value + 1 pcall(function() peopleHitStore:UpdateAsync(shooter.UserId, function(old) return (old or 0) + 1 end) end) end banana:Destroy() end end) -- Cleanup banana after 10 seconds if not already triggered. Debris:AddItem(banana, 10) end end)
DataStore & PeopleHit Update:
"PeopleHitStore"
.UpdateAsync
.Projectile Functionality:
Banana Ability:
applyKnockbackAndRagdoll
, updates the PeopleHit score for the shooter, and then destroys the banana.Customize values (like knockbackPower, upwardForce, and rotationPower) as needed to best suit your game’s gameplay and physics requirements.
make custom values for the banana
Below is the updated server‐side script. In this version, separate custom configuration values are defined for the banana ability. When a banana is touched, the script uses these custom values—such as bananaKnockbackPower, bananaUpwardForce, and bananaRotationPower—to apply a unique knockback, upward force, and angular spin effect before triggering the RagdollEvent and updating the PeopleHit score.
Ensure that you have a pre-made Banana part in ReplicatedStorage.
lualocal tool = script.Parent local Players = game:GetService("Players") local Debris = game:GetService("Debris") local ReplicatedStorage = game:GetService("ReplicatedStorage") local DataStoreService = game:GetService("DataStoreService") local peopleHitStore = DataStoreService:GetDataStore("PeopleHitStore") -- Projectile configuration (unchanged from original) local projectileSpeed = 100 local cooldownTime = 1.5 local firingDebounce = false local knockbackPower = 50 local upwardForce = 0.8 local rotationPower = 5 -- Custom Banana configuration values local bananaKnockbackPower = 75 -- Increased knockback for banana hits local bananaUpwardForce = 1.5 -- Stronger upward force for banana hits local bananaRotationPower = 10 -- More angular spin for banana hits --------------------------------------------------------- -- Function to apply knockback and trigger ragdoll (for projectile) --------------------------------------------------------- local function applyKnockbackAndRagdoll(character, hitDirection, origin) local ragdollEvent = character:WaitForChild("RagdollEvent") ragdollEvent:Fire(origin) local rootPart = character:FindFirstChild("HumanoidRootPart") if rootPart then local knockback = Instance.new("BodyVelocity") knockback.MaxForce = Vector3.new(1e5, 1e5, 1e5) knockback.Velocity = hitDirection * knockbackPower knockback.Parent = rootPart local spin = Instance.new("BodyAngularVelocity") spin.AngularVelocity = Vector3.new( math.random(-1, 1) * rotationPower, math.random(-1, 1) * rotationPower, math.random(-1, 1) * rotationPower ) spin.MaxTorque = Vector3.new(1e5, 1e5, 1e5) spin.Parent = rootPart Debris:AddItem(knockback, 0.5) Debris:AddItem(spin, 0.5) end end --------------------------------------------------------- -- Function to apply banana-specific knockback and trigger ragdoll --------------------------------------------------------- local function applyBananaKnockbackAndRagdoll(character, hitDirection, origin) local ragdollEvent = character:WaitForChild("RagdollEvent") ragdollEvent:Fire(origin) local rootPart = character:FindFirstChild("HumanoidRootPart") if rootPart then local knockback = Instance.new("BodyVelocity") knockback.MaxForce = Vector3.new(1e5, 1e5, 1e5) knockback.Velocity = hitDirection * bananaKnockbackPower knockback.Parent = rootPart local spin = Instance.new("BodyAngularVelocity") spin.AngularVelocity = Vector3.new( math.random(-1, 1) * bananaRotationPower, math.random(-1, 1) * bananaRotationPower, math.random(-1, 1) * bananaRotationPower ) spin.MaxTorque = Vector3.new(1e5, 1e5, 1e5) spin.Parent = rootPart Debris:AddItem(knockback, 0.5) Debris:AddItem(spin, 0.5) end end --------------------------------------------------------- -- Function to create and fire the projectile (shooting) --------------------------------------------------------- local function createProjectile(origin, direction, shooter) local projectile = Instance.new("Part") projectile.Shape = Enum.PartType.Ball projectile.Size = Vector3.new(2, 2, 2) projectile.CFrame = CFrame.new(origin) projectile.Color = Color3.new(1, 0, 0) projectile.Anchored = false projectile.CanCollide = false projectile.Parent = workspace local creator = Instance.new("ObjectValue") creator.Name = "creator" creator.Value = shooter creator.Parent = projectile local bv = Instance.new("BodyVelocity") bv.Velocity = direction * projectileSpeed bv.MaxForce = Vector3.new(1e5, 1e5, 1e5) bv.Parent = projectile projectile.Touched:Connect(function(hit) local character = hit.Parent if character and character:FindFirstChild("Humanoid") and character ~= shooter.Character then local hrp = character:FindFirstChild("HumanoidRootPart") if hrp then local hitDirection = (hrp.Position - origin).Unit applyKnockbackAndRagdoll(character, hitDirection, origin) end -- PeopleHit score update for projectile hits. if game.PrivateServerOwnerId == 0 and shooter and shooter:FindFirstChild("leaderstats") and shooter.leaderstats:FindFirstChild("PeopleHit") then shooter.leaderstats.PeopleHit.Value = shooter.leaderstats.PeopleHit.Value + 1 pcall(function() peopleHitStore:UpdateAsync(shooter.UserId, function(old) return (old or 0) + 1 end) end) end projectile:Destroy() end end) Debris:AddItem(projectile, 5) end --------------------------------------------------------- -- Tool activation (shooting) --------------------------------------------------------- tool.Activated:Connect(function() if firingDebounce then return end firingDebounce = true local character = tool.Parent local head = character:FindFirstChild("Head") local hrp = character:FindFirstChild("HumanoidRootPart") local shooter = Players:GetPlayerFromCharacter(character) if head and hrp and shooter then local origin = head.Position local direction = hrp.CFrame.LookVector createProjectile(origin, direction, shooter) end wait(cooldownTime) firingDebounce = false end) --------------------------------------------------------- -- Banana ability: Clone a Banana from ReplicatedStorage, -- spawn it on the ground, and trigger banana-specific knockback, ragdoll, -- and PeopleHit score update when touched. --------------------------------------------------------- local abilityEvent = tool:WaitForChild("AbilityEvent") -- RemoteEvent inside the tool abilityEvent.OnServerEvent:Connect(function(player) local character = player.Character if character and character.PrimaryPart then -- Compute a desired spawn offset (e.g. 5 studs ahead) local offset = character.PrimaryPart.CFrame.LookVector * 5 local desiredPosition = character.PrimaryPart.Position + offset -- Cast a ray downward from above the desired position to find the ground. local rayOrigin = desiredPosition + Vector3.new(0, 5, 0) local rayDirection = Vector3.new(0, -50, 0) local raycastResult = workspace:Raycast(rayOrigin, rayDirection) if raycastResult then desiredPosition = raycastResult.Position end -- Clone the Banana part from ReplicatedStorage. local bananaTemplate = ReplicatedStorage:WaitForChild("Banana") local banana = bananaTemplate:Clone() banana.Name = "Banana" banana.CFrame = CFrame.new(desiredPosition) banana.Parent = workspace -- Ensure the banana is affected by physics. banana.Anchored = false banana.CanCollide = true -- Add a Touched event to trigger banana-specific effects. local touched = false banana.Touched:Connect(function(hit) if touched then return end local hitCharacter = hit.Parent if hitCharacter and hitCharacter:FindFirstChild("Humanoid") and hitCharacter:FindFirstChild("RagdollEvent") then touched = true local hrp = hitCharacter:FindFirstChild("HumanoidRootPart") local hitDirection = Vector3.new(0, 0, 0) if hrp then hitDirection = (hrp.Position - banana.Position).Unit -- Add banana-specific upward force. hitDirection = (hitDirection + Vector3.new(0, bananaUpwardForce, 0)).Unit applyBananaKnockbackAndRagdoll(hitCharacter, hitDirection, banana.Position) else hitCharacter.RagdollEvent:Fire(banana.Position) end -- PeopleHit score update for banana ability. local shooter = player if game.PrivateServerOwnerId == 0 and shooter and shooter:FindFirstChild("leaderstats") and shooter.leaderstats:FindFirstChild("PeopleHit") then shooter.leaderstats.PeopleHit.Value = shooter.leaderstats.PeopleHit.Value + 1 pcall(function() peopleHitStore:UpdateAsync(shooter.UserId, function(old) return (old or 0) + 1 end) end) end banana:Destroy() end end) -- Cleanup banana after 10 seconds if not already triggered. Debris:AddItem(banana, 10) end end)
Custom Banana Values:
Separate Function for Banana Knockback:
applyBananaKnockbackAndRagdoll
, mirrors the original knockback functionality but uses the custom banana values.Banana Ability Logic:
applyBananaKnockbackAndRagdoll
, updates the shooter’s PeopleHit score, and destroys the banana.Projectile Functionality Remains Unchanged:
Adjust the custom values as needed to best fit your game's desired feel and balance.