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

How to fix error :TakeDamage isn't valid member of Player?

Asked by 5 years ago
Edited 5 years ago
player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:wait()
Humanoid = game:GetService("Players").LocalPlayer.Character
mouse = player:GetMouse()
if mouse then
mouse.KeyDown:connect(function(key)
if key=="b" then    
    game.ReplicatedStorage.Damage:FireServer(character.Humanoid,15)
end

I tried every method to work but It doesn't work anyways anyone know how to fix it?

I have script in remote event in server scripts

it have (Target and DMG) in the function.

serverside

game.ReplicatedStorage.Damage.OnServerEvent:Connect(function(Target,DMG)
    Target:TakeDamage(DMG)
end)
0
Post server codes too User#24403 69 — 5y
0
Post the real source code. "it have (Target and DMG) in the function." is not the source code. User#24403 69 — 5y

1 answer

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

Remember that the first argument that is passed to your listener is the client who fired the event. So Target was actually the player who fired your event and not what you passed to it. That also means DMG was the humanoid and not the number.

game.ReplicatedStorage.Damage.OnServerEvent:Connect(function(_, Target, DMG)
    --// the player parameter didn't serve a purpose so I used a placeholder variable `_`
    Target:TakeDamage(DMG);
end)

You should also consider using UserInputService.InputBegan rather than mouse.KeyDown since the latter is deprecated.

Sample code

local UserInputService = game:GetService("UserInputService");

UserInputService.InputBegan:Connect(function(input, gpe)
    if (gpe) then
        return; --// Interacted with game engine (e.g. typing in text box, clicked a GUI button); break out of function
    end
    print(input.UserInputType.Name); --// print the input type
end)
Ad

Answer this question