Hey everybody! I’m having an issue with on-click. My issue is that “while holding” never executes its body. The script is in ServerScriptService. Originally, the script was under a part called “Part”, but that didn’t work.
My code is:
local crosshair = game.Workspace.Crosshair
local part = game.Workspace.Part
local holding = false
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local weld = Instance.new("WeldConstraint",crosshair)
crosshair.CFrame = character.HumanoidRootPart.CFrame * CFrame.new(0,1,-10)
crosshair.Massless = true
crosshair.CanCollide = false
weld.Part0 = crosshair
weld.Part1 = character.HumanoidRootPart
end)
end)
local clickDet = Instance.new("ClickDetector")
clickDet.Parent = part
clickDet.MaxActivationDistance = 10
clickDet.MouseClick:Connect(function()
print("lmb")
holding = true
part.Anchored = true
end)
clickDet.RightMouseClick:Connect(function()
print("rmb")
holding = false
part.Anchored = false
end)
while holding do
print("holding")
part.CFrame = crosshair.CFrame
end
Thank you!
It never executes because when the script runs, holding is false, thus it will never work as by the time holding is true, the loop already ended. Solution is to put it in your MouseClick Event
clickDet.MouseClick:Connect(function()
print("lmb")
holding = true
part.Anchored = true
while holding do
print("holding")
part.CFrame = crosshair.CFrame
end
end)
1 Like
How would I make this work continuously without crashing?
Oh right, put a wait()
in the loop so it wont kill your game
clickDet.MouseClick:Connect(function()
print("lmb")
holding = true
part.Anchored = true
while holding do
print("holding")
part.CFrame = crosshair.CFrame
wait()
end
end)
Or better yet, instead of wait()
, do game:GetService("RunService").Heartbeat:Wait()
1 Like
Thank you for your help! I’ll let you know if I run into any more issues. I’ve marked you post as the Solution.
What would be the reason for using game:GetService(“RunService”).Heartbeat:Wait() instead of wait()?
It’s usually faster than wait()
but it’s frame rate locked I believe, so if there’s a lot of lag it’ll run slow. wait()
also has its own issues when a lot of them are ran at once. Typically you could use a Custom wait but I don’t think it’ll be that much of an issue for what you’re trying to do, but if there ever is any issues in terms of speed and stuff, do look into it
If you have anymore issues don’t be afraid to make another post!
1 Like
Thank you so much for the fast replies and help, I appreciate it!
1 Like
Hey, so I don’t know if you’re able to help with this, but:
I published the game, and it was completely broken, but worked absolutely fine in studio when I tested it. I clicked on the part, instead of it going to the “crosshair” part, it went to some random place in the sky. It’s anchored while it’s being held, if you’re wondering.
Wait huh? It should work, what’s going on?
I’m not sure… It just teleports upwards.
CanCollide is set to false while holding is true. If I remove the un-anchoring statements, it starts glitching through the ground.
Try tweaking some settings like the CanCollide and Anchored?
I’ll give it ago and let you known of the result, one second.
@EmbatTheHybrid Alright, didn’t work.
Never mind, forgot to publish haha. Thank you for the help!
1 Like