A constraint Elastic between a Player and an Entity

Hey Guys ;),

First of all, I’am new to Lua :-D.

So, I tryed to connect a Player with an Entity, after he pressed the USE Button.

So I have this simple test Plugin:

**Code: ** [lua]//for k,v in pairs(player.GetAll()) do
// v:SetNWInt(“canrope”, 1)
//end

function thinker()
for k,v in pairs(player.GetAll()) do
if v.IsPlayer() and v:KeyDown(IN_USE) and v:GetNWInt(“canrope”) == 1 then
lookat = v:GetEyeTrace().Entity
if lookat:IsValid() then
//v:SetNWInt(“canrope”, 0)
//Bone1 = v:GetPhysicsObjectNum(10)
//Bone2 = lookat:GetPhysicsObjectNum(10)
WPos1 = v:GetPos()
WPos2 = lookat:GetPos()
LPos1 = v:GetLocalPos()
LPos2 = lookat:GetLocalPos()
length = ( WPos1 - WPos2):Length()
//v:SetPos(WPos2)
local constraint, rope = constraint.Elastic( v, lookat, 0, 0, LPos1, LPos2, 0, 0, 0, “cable/cable2”, 4, 0 )
end
end
end
end

hook.Add(“Think”, “thinker”, thinker)[/lua]

But when i press the USE Button, i get strange results.

The Rope is far away from the Player and the Entity.

Can you help me?

Thanks

Have nobody an Idea :frowning:

Can Someone at least try it :slight_smile:

pussh

Don’t use GetLocalPos(), try this:

[lua]
local LPos1 = v:GetShootPos() --Will make the rope attach at the player’s head.
local LPos2 = v:GetEyeTrace().HitPos --Will make the other end attach wherever the trace hits.
[/lua]

Also, learn to localize your variables. All the variables and functions you are establishing in that script should be localized, which can be done by just putting the word local in front of them like I did above. Here’s some articles on localization:

Mh, not working :(, but better than before :wink:

This is my Code now:

** Code:**
[lua]
for k,v in pairs(player.GetAll()) do
v:SetNWInt(“canrope”, 1)
end

function thinker()
for k,v in pairs(player.GetAll()) do
if v.IsPlayer() and v:KeyDown(IN_USE) and v:GetNWInt(“canrope”) == 1 then
lookat = v:GetEyeTrace().Entity
if lookat:IsValid() then
v:SetNWInt(“canrope”, 0)
//Bone1 = v:GetPhysicsObjectNum(10)
//Bone2 = lookat:GetPhysicsObjectNum(10)
local WPos1 = v:GetPos()
local WPos2 = lookat:GetPos()
local LPos1 = v:GetShootPos()
local LPos2 = v:GetEyeTrace().HitPos
local length = ( WPos1 - WPos2):Length()
//v:SetPos(WPos2)
local constraint, rope = constraint.Elastic( v, lookat, 0, 0, LPos1, LPos2, 0, 0, 0, “cable/cable2”, 4, 0 )
end
end
end
end

hook.Add(“Think”, “thinker”, thinker)
[/lua]