Hi alll
I am making a horror game in which i was working on a magnitude script where
an sound would play if the antagonist is near but for some reason the script is not working
Here’s the script
local player = game.Players.LocalPlayer
local Doll = script.Parent
local target = player.Character
local magnitude = (Doll.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if magnitude < 10 then
game.Workspace.Impact:Play()
end
local player = game.Players.LocalPlayer
local Doll = script.Parent
local target = player.Character
while wait(1) do
local magnitude = (Doll.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if magnitude < 10 then
game.Workspace.Impact:Play()
else
print("player isn't enough close.")
end
end
Check the output for any errors. The only logical thing that I can see right now is that the script runs before the character has loaded so the character variable would just be nil
Actually do this
local player = game.Players.LocalPlayer
local Doll = script.Parent
local target = player.Character or player.CharacterAdded:Wait()
game:GetService("RunService").Heartbeat:Connect(funciton()
if game.Workspace.Impact.IsPlaying then
return
end
local magnitude = (Doll.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if magnitude < 10 then
game.Workspace.Impact:Play()
end
end)
local player = game.Players.LocalPlayer
local doll = script.Parent
local character = nil
player.CharacterAdded:Connect(function(character2)
character = character2
end)
while wait(1) do
if character ~= nil then
local magnitude = (doll.HumanoidRootPart.Position - character.HumanoidRootPart.Position).Magnitude
if magnitude > 10 then
workspace.Impact:Play()
end
else
print("There is no character")
end
end
Is this in a LocalScript or a Script [A server script]…? Because in a server script, LocalPlayer won’t work, and in a LocalScript, the whole thing wouldn’t work because of how rigs work, so you’d need to update your code, try doing a loop for the closest player every time it moves, for example, and play the sound there if it’s in a Server Script