Hello, I’m trying to make a panel that flings the character in the game, but it doesn’t work.
What’s the problem here?
local button = script.Parent
local targetPlayer = script.Parent.Parent.TargetPlayer.Text
local players = game.Players
button.MouseButton1Click:Connect(function()
if players:FindFirstChild(targetPlayer) then
game.Players(targetPlayer):FindFirstChild("Humanoid").Sit = true
local BodyVelocity = Instance.new("BodyVelocity", game.Players(targetPlayer):FindFirstChild("HumanoidRootPart"))
BodyVelocity.Velocity = Vector3.new(0,200,0)
wait(1)
BodyVelocity:Destroy()
end
end)
By the way, is the event necessary?
Because the console is clean
local button = script.Parent
local targetPlayer = script.Parent.Parent.TargetPlayer.Text
local players = game.Players
button.MouseButton1Click:Connect(function()
if players:FindFirstChild(targetPlayer) then
game.Players(targetPlayer).Character:FindFirstChild("Humanoid").Sit = true
local BodyVelocity = Instance.new("BodyVelocity", game.Players(targetPlayer).Character:FindFirstChild("HumanoidRootPart"))
BodyVelocity.Velocity = Vector3.new(0,200,0)
wait(1)
BodyVelocity:Destroy()
end
end)
local button = script.Parent
local targetPlayer = script.Parent.Parent.TargetPlayer.Text
local players = game.Players
button.MouseButton1Click:Connect(function()
if players:FindFirstChild(targetPlayer) then
game.Players[targetPlayer].Character:FindFirstChild("Humanoid").Sit = true
local BodyVelocity = Instance.new("BodyVelocity", game.Players[targetPlayer].Character:FindFirstChild("HumanoidRootPart"))
BodyVelocity.Velocity = Vector3.new(0,200,0)
wait(1)
BodyVelocity:Destroy()
end
end)
Your code wont function for a majority of issues, it only runs on the client, the forces are way too small, the targetPlayer variable remains constant, and you’re directly indexing the Players service without using FindFirstChild. Instead remote events should be used to send a “fling user” request to the server to fling the selected user, and the issues listed above should be fixed:
--Client, your UI
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Remote = ReplicatedStorage:WaitForChild("FlingRemote")
local button = script.Parent
local targetPlayer = button.Parent.TargetPlayer --using .Text here makes the variable constant!
button.MouseButton1Click:Connect(function()
local target = Players:FindFirstChild(targetPlayer.Text)
if not target then return end
Remote:FireServer(target)
end)
--Server, Script is ServerScriptService
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remote = Instance.new("RemoteEvent", ReplicatedStorage)
Remote.Name = "FlingRemote"
local admins = {game.CreatorId, 1, 2, 3, 4, 5} --userIds of people allowed to fling others
local smallest = 10000 --smallest force
local max = smallest*1000 --max force
Remote.OnServerEvent:Connect(function(player, target)
--when using remotes, you have to make checks ON THE SERVER to ensure the client has permission to perform an action
if not table.find(admins, player.UserId) then
warn(player.Name.." is not an admin!")
return
end
local Character = target.Character
Character.Humanoid.Sit = true
local bv = Instance.new("BodyVelocity", Character.HumanoidRootPart)
local r = math.random
bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge) --set the max force to infinity
--set the velocity to a random huge value, 200 is nothing when it comes to forces
bv.Velocity = Vector3.new(r(smallest, max), r(smallest, max)*workspace.Gravity, r(smallest, max))
task.wait(1) --use the new task library!
bv:Destroy()
end)