Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Sword damage through server to NPCs?

Asked by 5 years ago
Edited 5 years ago

Im creating a RPG that gives the player stats like strength, dexterity and such and I want these stats to affect sword and weapon damage. Ive learned the basics of remote events and how to send values to a server script by reading over a free model shop script, but I have no clue how to send to the server an enemys humanoid and how to have the server actually take health from them.

local sword script

script.Parent.Changed:Wait(.3)
local tool = script.Parent
local config = tool.config
local minda = config.minda
local maxda = config.maxda
local player = game.Players.LocalPlayer
local stats = player:WaitForChild("playerstats")
local slashanim = script.Parent.anim:WaitForChild("SlashAnim")
local slashanim2 = script.Parent.anim:WaitForChild("SlashAnim2")
local slashsound = script.Parent.Handle:WaitForChild("swordsound")
local player = game.Players.LocalPlayer
local Character = script.Parent.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local sword = script.Parent
local blade = sword.blade
print "hey"
local damage = game.ReplicatedStorage.remotes.damage:WaitForChild("normal")
script.Parent.Activated:Connect(function(attack)
    slashsound:Play()
    damage:FireServer(minda.Value, maxda.Value)
print "hey1"
end)    
print "hey2"

minda and maxda are minimal damage and max damage

server side script

local normal = game.ReplicatedStorage.remotes.damage.normal
normal.OnServerEvent:Connect(function(player, minda, maxda)
    local stats = player.playerstats
    local strn = stats.strength
    local dex = stats.dexterity
    local focus = stats.focus
    local health = stats.health
    local manac = stats.manac

end)

I dont have much going on because I want to know how to send the damage value before i continue. God bless friends

Edit : Here is the correct code for anyone with the same problem

local damage = game.ReplicatedStorage.RemoteEvents.damage --set to whereever your damage remote event is
damage.OnServerEvent:Connect(function(player, weapondamage, enemyattacked) --weapon damage refers to the tools damage you set and enemyattacked is the npc/player touched

    local stats = player.playerstats --or leaderstats
    local strn = stats.strength --these can be whatever you want to affect damage
    local dex = stats.dexterity --these can be whatever you want to affect damage
    local Humanoid = enemyattacked.Character.Humanoid
    local damage = weapondamage + strn.Value --or dex.Value. we didnt do weapondamage.value because in the tool, we sent weapondamage.Value

    Humanoid:TakeDamage(damage) --You could just have the humanoid(the enemyattacked) health set, but takedamage is the proper way, not allow you to attack through forcefields and such.
end)

if you dont want your weapon to deal damage to other players, simple added

if not game:GetService("Players"):GetPlayerFromCharacter(enemyattacked.Parent) then

above "local stats=..." and an extra end. God bless mates

0
answering RetroGalacticGamer 331 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Ok so it is actually quite simple. If you want to send the humanoid, you need to reference the enemy's player first. I don't think you already have a variable for the enemy's player, so I will make one. First, add a touched event to the LocalScript. Blade.Touched:Connect(function(hit) local EnemyCharacter = hit.Parent local EnemyPlayer = game.Players:GetPlayerFromCharacter(EnemyCharacter) if EnemyPlayer then --put and other arguments in here damage:FireServer(minda.Value, maxda.Value,EnemyPlayer) end end)

Second, change your ServerScript to look like this:

local normal = game.ReplicatedStorage.remotes.damage.normal
normal.OnServerEvent:Connect(function(player, minda, maxda, EnemyPlayer)
    local stats = player.playerstats
    local strn = stats.strength
    local dex = stats.dexterity
    local focus = stats.focus
    local health = stats.health
    local manac = stats.manac
    local Humanoid = EnemyPlayer.Character.Humanoid
    Humanoid.Health = 0 --You decide this value
end)



I hope I answered your question and this works for you. Sincerely, DominusInfinitus

0
Thank you so much, seems like Im making things more difficult than they actually are.. that and not knowing what im doing, lol. Also, does this include npcs, Im not planning on adding PVP to my game yet. God bless, friend. Pinetall 4 — 5y
0
stupid question at the end... Thank you so much for all the help :)))) Pinetall 4 — 5y
0
Huh? RetroGalacticGamer 331 — 5y
0
what do you mean RetroGalacticGamer 331 — 5y
View all comments (7 more)
0
You should damage the humanoid, not set its health. XX_Scripta 11 — 5y
0
:( dude he can do that part RetroGalacticGamer 331 — 5y
0
I already gave him a template RetroGalacticGamer 331 — 5y
0
what do you mean damage the humanoid? I did humanoid.health = humanoid.health - damage Pinetall 4 — 5y
0
If my answer helped, can you accept it please? RetroGalacticGamer 331 — 5y
0
Although your code would of caused tons of problems and headaches down the road and you were rude with your "dude he can put that part" instead of telling me how, ill accept your answer since it helped out. Pinetall 4 — 5y
0
thanks. RetroGalacticGamer 331 — 5y
Ad

Answer this question