The part is supposed to move to the mouse's position and it worked almost fine but the thing is it moves towards my screen...
Local script:
local moveevent = script:WaitForChild("MoveEvent")
script.Parent.Parent.Equipped:Connect(function(mouse)
mouse.Move:Connect(function()
moveevent:FireServer(mouse.Hit.p)
end)
end)
Script:
local moveevent = script.Parent:WaitForChild("MoveEvent")
moveevent.OnServerEvent:Connect(function(plr, mousepos)
workspace:WaitForChild("Spawnpart").Position = mousepos
end)
In local script:
mouse.TargetFilter = workspace:WaitForChild("Spawnpart")
What's happening is that your mouse it hitting the part instead of what's behind it. Read more
Related
I am not sure where to start so asking for some help. I want to create a script that detect if a certain mesh is hit by players right hand. Every time the mesh is hit I want to increase points for player by 1.
Anyone who can nudge me in the right direction here?
Thanks!
EDIT:
I have added this script in StarterCharacterScripts:
game.Players.LocalPlayer.Character:WaitForChild("RightHand").Touched:Connect(function(hit)
local part1 = workspace.CoinsClouds["Meshes/SackOfGoldNoCoins1"]
part1.Touched:Connect(function(hit)
if hit.Name == "RightHand" then
print(hit.Name)
end
end)
end)
This will register when I bump into the part with the right hand, BUT it will register it 5-20 times in a split second every time I bump into the part with the right hand. See attached image. Anyone know why? I would like it to register only once when the right hand is bumped against the part, or even better, only when the user punch the part/mesh. I have tried to add a wait after RightHand is found, but that doesn't work.
PS! I don't know if this is the right way to script it...
Below is an example of the code you would use. It will check if your part is touched by either the right or left hand and it will run whatever is inside of your if statement, in this case it will increase whatever your score is by 1.
local part = workspace.Part -- reference the part here
local debounce = false --used to check for debounce
part.Touched:Connect(function(hit)
if hit.Name == "RightHand" or "LeftHand" then -- checks if a hand touched it
if not debounce then --Check that debounce variable is not true
debounce = true --currently touching the part
print("hit")
wait(1)
debounce = false
end
end
end)
In relation to your new question it is to do with debounce. Roblox has an entire article on it here: https://developer.roblox.com/en-us/articles/Debounce
Basically you would have to add a new if statement to check that the player isn't already touching this part if it is it wont repeat the what is inside the if statement. The code sample above has been edited to work with debounce.
How do you teleport characters in Roblox without causing the HumanoidRootPart to be out of sync with the character? I have found that moving just the HumanoidRootPart will cause the rest of the character to snap back in place where you want them to be, but to the server, it is still in place before the teleport. Is there a better method?
Yes, you can use model:MoveTo() to move models (characters are models).
For example, a simple teleporter:
local debounce = false
-- the teleporter is just a part with the script it's child
script.Parent.Touched:Connect(function (hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player and not debounce then
debounce = true
local char = player.Character
local rootPart = char:WaitForChild("HumanoidRootPart")
char:MoveTo(rootPart.Position + Vector3.new(0, 10, 0))
wait(.5)
debounce = false
end
end)
I'm trying to make the part follow the mouse's Position Globally via remoteEvents. But i got this error
workspace.Tool.Handle.Script6: invalid argument#3(Vector3 Expected, Got Instance)
is there something wrong?
Local Script:
local tool = script.Parent.Parent
local rEvent = script.Parent.MoveToMousePos
tool.Equipped:Connect(function(mouse)
mouse.Move:Connect(function()
rEvent:FireServer(mouse.Hit.p)
end)
end)
Script:
local tool = script.Parent.Parent
local rEvent = script.Parent.MoveToMousePos
local part = workspace.Test
rEvent.OnServerEvent:Connect(function(mousePos)
part.Position = mousePos
end)
The error message is telling you that assignment to part.Position is failing because the supplied value is not a Vector3, but is instead an Instance. So looking at how the mousePos variable is assigned is the clue to the problem.
Whenever a client fires a RemoteEvent, the OnServerEvent supplies the Player instance that sent the message, and then all of the arguments. So currently, the mouse position is being sent up to the server Script, but it is being ignored in the function signature, and the mousePos variable is being assigned as the Player that called RemoteEvent.FireServer().
To fix your code, just add a variable to account for the player that called the RemoteEvent.
rEvent.OnServerEvent:Connect(function(player, mousePos)
part.Position = mousePos
end)
I use MoveTo for following the position and change the CFrame for rotation but it looks supper jaggy. I think the problem is that the code is on the server-side and not on the client-side since I want other players to be able to see the object following the player
Try using weldConstrain. You can create it by code and connect 2 parts to it. The parts are going to stay at the same relative position from each other. Here is a link for the API reference about the subject: https://developer.roblox.com/en-us/api-reference/class/WeldConstraint
Here is an example code that adds a part at the top of a player's head when it joins the game:
local Players = game:GetService('Players') -- get's the players service
local function OnPlayerAdded(player)
local PlayerCharacter = player.Character
-- waits until the player's character loads
while PlayerCharacter == nil do
wait(1)
PlayerCharacter = player.Character
end
-- Get's the player's model in workspace
local PlayerModel = workspace:WaitForChild(PlayerCharacter.Name)
-- Get's the player's head inside it's model
local PlayerHead = PlayerModel:WaitForChild('Head')
-- Write the path to the part you want to follow the player, bellow
local FollowPartOriginal = workspace:WaitForChild('Part')
-- If you want to clone the part, so more players can be followed by it,
-- don't delete the line below, if you want only 1 part, delete the line below.
local FollowPart = FollowPartOriginal:Clone()
-- set's the part position on top of the player's head, with an offset
FollowPart.Position = PlayerHead.CFrame.p + Vector3.new(0,2,0)
FollowPart.Parent = workspace
-- creates a weldconstrain
local WeldConstraint = Instance.new('WeldConstraint')
-- connect's the weld to the player's head and the other part
WeldConstraint.Part0 = PlayerHead
WeldConstraint.Part1 = FollowPart
WeldConstraint.Parent = PlayerHead
end
Players.PlayerAdded:Connect(OnPlayerAdded)
Place this code in a script(not a local script).
If you use the function separately, don't forget to put the player parameter. The parameter needs to contain the player Instance(ex: game.Players.PlayerName)
To change the part position, use the offset in this line of the code:
FollowPart.Position = PlayerHead.CFrame.p + Vector3.new(write offset here)
If you want the part the stop following the player, make a script to delete the part, or the weld constrain(child of the player's head)
I am using a script to set cancollide = true and transparency = 0, but i cant climb the truss. but when I'm already in testing mode (in studio) and i drag in the same truss, i can climb it, and i am looking at properties, its same, both anchored is true, they are touching same parts, i dont know why this is happening. Please help thanks :)by the way, i am making tycoon and this is script i am using:
wait(1)
amount = 0 -- cost of model
owner = script.Parent.Parent.Owner
local stun = false
pcall(script.Parent.Head.Touched:connect(function(hit)
if hit.Parent ~= nil then
player = game.Players:findFirstChild(hit.Parent.Name)
if not stun and player ~= nil then
if player.Name == owner.Value then
if player:findFirstChild("leaderstats") ~= nil then
stats = player:findFirstChild("leaderstats")
if stats.Money.Value >= amount then
stun = true
stats.Money.Value = stats.Money.Value - amount
script.Parent.ladder.CanCollide = true
script.Parent.ladder.Transparency = (0)
script.Parent.Head:Remove()
wait(1)
stun = false
end
end
end
end
end
end))
dont worry about other stuff, it works, its just this part that matters now:
script.Parent.ladder.CanCollide = true
script.Parent.ladder.Transparency = (0)
script.Parent.Head:Remove()
Please help :( this is the problem with the ladder using script not working and same dragged in from toolbox truss working, iv done this with many trusses and ladders and same result :(
So, a error I noticed in your code, is that you erase your Character's Head, which would kill your character, or possibly throw a error if the .Touched event fires two times.
There are many efficency errors in this script, such as .Transparency = (0), or doing script.Parent.ladder instead of using variables, but it really is no problem most of the times. Something you could try, is to use Instance.new() to create the ladder.If this game is FilteringEnabled on (Expermiental mode off), please note your script won't work as intended if it is a LocalScript, it will only work well on Server-Side (aka, a normal Script instead of a LocalScript)